Understanding Replace Rule (Switch Hosts)

Requestly
Requestly
Published in
3 min readMar 11, 2024

--

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:

  1. Go to HTTP Rules panel from the left side menu of Requestly app.
  2. Click on ‘New Rule’ button to open a list of available rules.
  3. 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.
  4. Enter values of Source Condition, Replace & With, and hit Save rule.
  5. 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:

  1. Create a new replace rule.
  2. Use prod hostname as source condition, or you can keep it empty.
  3. In Replace field enter the production API URL — https://prodapi.example.com
  4. In With field enter the stag API URL — https://stagapi.example.com
  5. 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.

  1. Create a new replace rule.
  2. Use prod hostname as the source condition, or you can keep it empty.
  3. In Replace field enter the production API URL — https://api.example.com/v1/
  4. 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.
  5. 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:

  1. Create a new replace rule.
  2. Use source condition to match the URL of minified JS.
  3. In Replace field enter — .min.js
  4. In With field enter — .js
  5. 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.

--

--

Requestly
Requestly

Setup redirects, modify headers, switch hosts, insert user scripts and much more. Install it on Chrome & Firefox and join our family of more than 100,000 devs!