Understanding Replace Rule (Switch Hosts)
Replace Rule enables you to replace a String in URL with another String. This feature is particularly useful for developers who want to swap the API endpoints from one environment to another or change something specific in the URL. Requests are matched with source condition, find and replace
is performed on those requests by redirecting to the resulting URL.
Video Guide
Step-by-Step Guide
Follow these steps to configure a Replace Rule in the Requestly App:
- Go to HTTP Rules panel from the left side menu of Requestly app.
- Click on ‘New Rule’ button to open a list of available rules.
- Select the ‘Replace String’ from the list of rules to open a detailed screen with more info about replace rule. Click
Create Rule
button on the top right. - Enter values of Source Condition, Replace & With, and hit Save rule.
- Use the Test this rule window that appears after saving the rule to validate its functionality.
Use Cases
Load staging APIs on production front-end service
Before shipping a bug fix or a feature, it’s better to check it against production data. By using Replace Rule, we can test staging APIs with Production. Here are the steps for the same:
- Create a new replace rule.
- Use prod hostname as source condition, or you can keep it empty.
- In
Replace
field enter the production API URL —https://prodapi.example.com
- In
With
field enter the stag API URL —https://stagapi.example.com
- Save the Rule and test.
Test a different version of API on Production/Staging
Using steps similar to the ones above, we can even change the version of APIs on a production website.
- Create a new replace rule.
- Use prod hostname as the source condition, or you can keep it empty.
- In
Replace
field enter the production API URL —https://api.example.com/v1/
- In
With
field enter the stag API URL —https://api.example.com/v2/
You can use it with the path as well if only a few APIs need to be checked with the new version. - Save the Rule and test.
Debug with a non-minified version of a script on Prod
Debugging a prod site with minified versions of JS is difficult. Using the replace rule, we can load a non-minified version of the JS to debug faster:
- Create a new replace rule.
- Use source condition to match the URL of minified JS.
- In
Replace
field enter —.min.js
- In
With
field enter —.js
- Save the Rule and test.
Similarly, any part of a URL can be replaced using Replace Rule of Requestly.
Troubleshooting
- Replace & Redirect Rule doesn’t work in case of CORS redirect for Chrome Browser 119+ Versions. Authorization headers are dropped on the CORS redirect. This is a known issue, it's due to a change in the way Chrome handles redirection post-v119 release. We are currently fixing the bug. You can track the status on Github — Link
- There are some other cases where rule might not work as expected, visit our troubleshooting guide for more details.