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 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 resulting URL.
Content Overview
Video Guide
Step-by-Step Guide
Follow these steps to configure a Replace Rule in the Requestly App:
- Go to HTTP Rules panel from 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.
Options
Below are details related to each option present on Replace Rule Screen:
- Source Condition — (default: empty) It’s a matching condition for identifying the HTTP Requests on which the replace rule is to be applied. Source condition can be applied based on URL, Hostname, or Path by using different matching criteria like Contains, Equals, Regex match, and Wildcard Match.
- Source Filters — Source Condition can be further refined using Source Filters. You can manage Source Filters by clicking the filter icon next to the Source Condition field.
- Replace — A text that needs to be replaced.
- With — A text which needs to be placed in place of replace text.
- Enable — Toggle status to enable/disable a rule.
- Group — Rules can be grouped and shared with team members for better collaboration.
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 similar steps as above we can even change the version of APIs on a production website.
- 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://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 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
If the rule is not working as expected please visit our troubleshooting guide for more details.