Understanding Redirect Rule

Dinesh Thakur

Introduction

The Redirect Rule in Requestly enables users to re-route web requests from one URL to another. This functionality is particularly valuable for developers and testers who need to manipulate network traffic for debugging, testing, or development purposes.

Content Overview

Video Guide

Step-by-Step Guide

To fully utilize the feature, let’s walk through the setup process:

  1. Start by installing Requestly extension in your browser.
  2. Go to HTTP Rules panel from the left side menu of the Requestly app.
  3. Select the ‘Redirect rule’ from the list of rules to open a detailed screen with more info about the Redirect rule. Click Create Rule button on the top right
  4. Enter the source condition to match the url using one of the built-in function from Equals, Contains, RegEx or Wildcard
  5. Optionally you can test your source condition using the Test URL condition button.
    • To test enter the url you want to check
    • You will be able to see if the url is matching and will get variables you can use for redirection
  6. Now Select the type of redirection you want
    • Redirect to another URL
    • Map Local (Desktop app only)
    • Redirect to Mock server (upload file)
  7. Based on the type of redirection enter the file/URL you want to redirect the request to.
  8. Use the Test this rule window that appears after saving the rule to validate its functionality.

Options

  1. Source Condition: If the request URL matches this source condition, then only the rule is applied. You can use URLHost, or Path with RegexContainsWildcard or Equals to match the source request. Learn more about source conditions here.
  2. Source Filters: This helps you define better targeting conditions and restrict rules to be applied on specific webpages (or domains), request types, request methods, or request payloads. Learn more about source filters here.
  3. Types of Redirects :
    1. URL Redirect : In this you mode you can enter the url where the matched request should be redirected to
    2. Map Local : In this mode you can redirect the request to any local file (works only in desktop app)
    3. Mock Server : In this mode you can use or add any file from your mock server on requestly
  4. Test this rule – This box will appear only after you save the rule, it accepts a URL and opens it in a new tab for you to test. It comes with an option to capture screen, console & network details, if enabled Requestly will capture these details in SessionBook for debugging later.

Use Cases

Redirect to local server:

In Requestly, you can create a Redirect Rule to direct requests to a specific local development server by specifying the original URL as the pattern and the local server URL as the redirect destination. This allows you to test changes locally without impacting the live production environment. Additionally, you can map production scripts or APIs to your local running code, enabling direct testing of local code on production sites without the need for deployment. Here are the steps you can follow for the same:

  1. Create a new Redirect Rule.
  2. Use your API hostname & path as source conditions like – company.com/api/path
  3. In Redirect URL field enter your local development URL like – localhost:8000.
  4. Save the Rule & Test.

Serving Local JSON as Sample Data for Development

Use Requestly to redirect API requests to locally stored JSON files, providing realistic sample data for front-end development and testing without relying on external servers. Here are the steps you can follow for the same:

  1. Create a new Redirect Rule.
  2. Use your API hostname & path as source conditions like – company.com/api/path
  3. in redirect select Mock Server and upload your json file
  4. Save the Rule & Test.

Test API version changes

Suppose there is a new version of some API which you are using. To test the backward compatibility of the API, you can just set up a redirect rule to redirect all URLs from the older version to the newer version and check if your application behaves as expected. This way, you don’t need to change a single line of your code and test the API upgrades.

  1. Create a new Redirect Rule
  2. Use your API hostname and path with wildcards in source conditions like – *://company.com/api/v0/*
  3. Enter the wildcard pattern to redirect the request keeping query parameters and url paths like – $1://company.com/api/v1/$2
  4. Save the Rule & Test.

Swap Tag Manager scripts from production to staging / dev environment

Using a Redirect Rule in Requestly, you can load unminified implementations from libraries like Bootstrap and jQuery for testing purposes this allows you to seamlessly switch between minified and unminified versions as needed during development, ensuring smoother troubleshooting and optimization of your web applications.

  1. Create a new Redirect Rule
  2. Enter the URL of minified version of your libraries in source condition for eg https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css
  3. Now redirect it to unminified version of the file eg – https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.css
  4. Save the Rule & Test.

Troubleshooting

There are some cases where rules might not work as expected, visit our troubleshooting guide for more details.

Written by
Dinesh Thakur
Dinesh Thakur, fascinated by technology since childhood, has mastered programming through dedication. Whether working solo or in a team, he thrives on challenges, crafting innovative solutions.

Related posts