Replace String Rule

Learn how to modify URLs, query parameters, or content dynamically with Requestly's Replace String Rule. Test and debug changes without altering your code.


The Replace String Rule in Requestly allows you to modify the content of HTTP requests and responses, making it easy to test, debug, or apply fixes dynamically without modifying your original codebase. This feature is useful for replacing URLs, headers, or specific content in requests and responses.

Why Use the Replace String Rule?

  • Switch Domains: To test your app with the new changes done on the local server, you can use replace your-app-server.com with localhost using Replace String Rule. In this way, you can test your app without having to change any of your code.

  • Change Query Parameter Values: You can change the values of query parameters present in a URL. For example: Changing ?dl-0 to ?dl=1 in a Dropbox URL downloads the file directly without going to the usual preview page.

  • Test Mobile Versions: Replace desktop-specific resources with mobile-optimized versions of scripts, images, or APIs, to see how your website responds on mobile without deploying changes.

How to Configure the Replace String Rule

1

Open HTTP Rules and Create a New Rule

Navigate to the HTTP Rules tab in the Requestly web or desktop app. Click on + New Rule and select the Replace String option.

2

Name and Describe Your Rule

Provide a descriptive name for your rule to keep things organized. Optionally, add a brief description to explain its purpose for easy identification later.

3

Define the Source Condition

Define the criteria for the rule to be applied, such as URL, Host, or Path. Choose from Regex, Contains, Wildcard, or Equals, or use a Filter for more precise conditions.

For more detailed guidance, refer to the Source Conditions Guide.

Example:

For this example, we are using the Requestly Playground to demonstrate the Replace Request rule. This gives you a near-live experience of how the rule works. In this case, we are targeting API requests that fetch product data.

To replace the API endpoint to fetch products

  • Condition Type: URL

  • Operator: Contains

  • Value: https://mock.redq.io/api/products

4

Replace Part of the URL

In this step, configure the Replace String Rule with the following two sections:

  1. Replace: Define the part of the URL you want to replace. This could be the hostname, query value, or any specific part of the URL.

    Example: type.slug:bakery

  2. With: Specify the value or string that will replace the original part of the URL.

    Example: type.slug:grocery

This rule ensures that any requests matching the given criteria will have the specified URL part replaced dynamically.

5

Save and Activate the Rule

Once satisfied with your setup, click Save to store the rule. Don’t forget to toggle it ON to activate it and apply the changes.

6

Test the Rule

To verify the rule works as expected, click the Test button, enter a relevant URL, and then click Test Rule. You can use the following URL to test the redirection: https://www.requestly-playground.com/?rq-category=replace

Initial State

Final Result

Updated on