3xx responses by transforming the incoming URL using a regular expression and optional CEL interpolation.
It can modify any part of the URL such as scheme, host, port, path, or query, and returns a redirect via the Location header.
This action is different from the URL Rewrite Action, which changes the request internally while keeping the client’s URL unchanged.
Redirects, by contrast, instruct the client to issue a new request to the new URL.
Configuration Reference
The Traffic Policy configuration reference for this action.Supported Phases
on_http_request
Type
redirect
Configuration Fields
A regular expression pattern used to match a part of the URL.
A regular expression pattern used to replace the matched part of the URL.
A
3xx status code used for redirecting.Map of key-value headers to be added to the response.
Maximum
10 headers can be specified.Behavior
The Redirect Action modifies the request URL and returns an HTTP redirect (3xx) response.
It performs the following transformation in sequence:
- Evaluate any CEL interpolation
- Apply the regular expression match (
from) - Replace the matched text or URL with the
tovalue - Send the redirect response
CEL interpolation
CEL expressions in${...} are evaluated before the redirect rule is applied. Interpolations can reference request or connection data.
For example:
Matching
Thefrom field is a regular expression evaluated against the entire URL (scheme, host, port, path, query, and fragment). Matching is not anchored by default; a substring match anywhere in the URL triggers the redirect.
Example
The following example redirects any request that contains/api/v1/ to /api/v2/. The redirect is applied to the entire URL, not just the path.
https://example.com/foobar/api/v1/users would be redirected to https://example.com/foobar/api/v2/users.
Anchoring / Path Prefix
Anchoring with^/api/v1/ will not match paths that include the full scheme. To match only the path, use one of the following methods:
Replacement behavior
Theto value replaces what was matched by from.
The replacement is relative to the from match.
When from is omitted, the redirect action replaces the entire URL.
Capture groups (such as $1, $2) can be used to insert matched text into the replacement.
When from matches a substring, only that segment changes:
https://example.com/api/v1/users would be redirected to https://example.com/api/v2/users.
Relative vs. absolute replacements
Theto value is applied relative to the from match. When from matches part of the URL, only that segment is replaced. When from is omitted, ngrok treats the entire URL as matched and replaces it absolutely.
Relative example
https://example.com/old/foo would become https://example.com/https://new.example.com/blog/foo.
Absolute example
When a partial match exists, the replacement is relative; when no from match is defined, it is absolute:
https://example.com would be redirected to https://new.example.com/blog.
Redirecting to another host
To redirect cleanly to a new host, match the entire URL:
https://example.com/old/foo would be redirected to https://new.example.com/new/foo.
If the rewritten URL equals the original, ngrok skips the redirect.
Redirect response
After replacement, the redirect action sends an HTTP redirect with the computedLocation header. The default status code is 302 Found.
| Code | Description | Behavior |
|---|---|---|
| 301 | Moved Permanently | Clients may cache the redirect. |
| 302 | Found (default) | Temporary redirect; some clients change the request method to GET. |
| 303 | See Other | Forces the next request to use GET. |
| 307 | Temporary Redirect | Preserves the original request method and body. |
| 308 | Permanent Redirect | Preserves the original request method and body. |
Examples
Redirect using Paths
The following Traffic Policy configuration demonstrates how to use theredirect action to redirect incoming requests from /products to /store/products.
Example Traffic Policy Document
/products to /store/products with the default 302 Found status code.
Example request
/products will be redirected to /store/products with a 302 Found status code, and the Location header will indicate the new URL.
Redirect using Regular Expressions
The following Traffic Policy configuration demonstrates how to use theredirect action to redirects requests from an old API endpoint to a new one.
Example Traffic Policy Document
/api/v1/users?id=([0-9]+) and redirect them to /api/v2/users/$1/ with a 301 status code. Additionally, a custom header x-redirected: true is added to the response.
Example Request
/api/v2/users/123/, with a 301 Moved Permanently status and a custom header.
Redirect with CEL Interpolation
The following Traffic Policy configuration demonstrates how to use theredirect action to redirect users while using CEL Interpolation.
Example Traffic Policy Document
/api/v2/geo or /api/v2/geo/ to /api/v2/geo?city=${conn.geo.city} using CEL Interpolation to insert the city from the connection’s geolocation data.
Example Request
https://example.ngrok.app/api/v2/geo will be redirected to https://example.ngrok.app/api/v2/geo?city=San Francisco, with the 302 Found status code and the Location header indicating the new URL that includes the city from the connection’s geolocation data.
Action Result Variables
The following variables are made available for use in subsequent expressions and CEL interpolations after the action has run. Variable values will only apply to the last action execution, results are not concatenated.A list of elements that were matched during redirection. These represent the request components (for example, path or query parameters) that triggered the action and resulted in the redirect.
The URL to which the traffic was redirected. This is the destination URL returned as part of the redirect response after the action was executed.
A machine-readable code describing an error that occurred during the action’s execution.
A human-readable message providing details about an error that occurred during the action’s execution.