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:

  1. Go to HTTP Rules panel from 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.

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:

  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 similar steps as above we can even change the version of APIs on a production website.

  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://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 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. Authorisation headers are dropped on CORS redirect. This is a known issue, its due to a change in the way Chrome handle 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.

This article was written by:

Abhishek Sachan

Abhishek Sachan

Abhishek is Growth Engineer at Requestly and has profound love for programming.

Share this article:

You may also like