Location
header.
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. Default is 302
.Map of key-value headers to be added to the response.
Maximum
10
headers can be specified.Behavior
If the specifiedfrom
regular expression matches the request URL, the URL will
be modified according to the to
field. If no from
field is specified, the
entire URL will be treated as a match.
When a match is found and the request URL is modified by the to
field, the
action will return a temporary 302
redirect by default, unless specified
otherwise through the status_code
field.
If the request URL is not changed, the action will take no further action.
Note about CEL Interpolation
All CEL interpolation will occur before regular expressions are evaluated.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 (e.g., 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.