Understanding Replace Rule (Switch Hosts)

Abhishek Sachan
Replace Rules enables changes in a URL on the fly using pattern matching for testing and debugging purposes. This feature is particularly useful for developers testing the web page behavior under different conditions without altering the codebase directly.

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

If the rule is not working as expected please visit our troubleshooting guide for more details.

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

Contents​

Subscribe for latest updates​

Share this article

Related posts