Redirect
Overview
The Redirect policy action enables transforming your URL using regular expressions and redirect through the "location" header.
Example
Traffic Policy for redirecting to a url with a different version
and id
param.
- YAML
- JSON
# snippet
---
actions:
- type: "redirect"
config:
format: "ngrok"
from: "/api/v1/users/([0-9]+)"
to: "/api/v2/users?id=$1"
status_code: 301
headers:
x-custom-header: "custom-value"
// snippet
{
"actions": [
{
"type": "redirect",
"config": {
"format": "ngrok",
"from": "/api/v1/users/([0-9]+)",
"to": "/api/v2/users?id=$1",
"status_code": 301,
"headers": {
"x-custom-header": "custom-value"
}
}
}
]
}
Request:
curl https://example.ngrok.app/api/v1/users/42
Result:
< HTTP/2 301
< location: https://example.ngrok.app/api/v2/users?id=42
< x-custom-header: custom-value
Behavior
When executed this action will abort the request and replace all matches on the inbound URL with the configured replacements. Before regex replacement, all variables or expressions will be replaced with their corresponding values using cel expressions templating syntax.
Reference
Supported Directions
- Inbound
Configuration
Type |
---|
redirect |
Parameter | Description | |
---|---|---|
from | string | A regular expression string to match inside of the URL. Supports interpolating the results of cel expressions outside of regular regex. |
to | string | A regular expression string to replace the from match with. Supports interpolating the results of cel expressions outside of regular regex. |
status_code | int | 3xx status code used for redirecting, 302 by default. |
headers | Map<string, string> | Headers to be added to the response. |
format | string | (default: ngrok ) Determines interpolation format in to /from fields. |
Templating Syntax
The results of CEL expressions can be interpolated into your policy's config
using ngrok's ${}
templating syntax. For a complete list of available variables and functions or to see a more detailed explanation, checkout the docs.
More Examples
Using "to" field with templating syntax
- YAML
- JSON
# snippet
---
actions:
- type: "redirect"
config:
format: "ngrok"
to: "/api/v1/carmen/sandiego/${conn.geo.city}"
status_code: 301
// snippet
{
"actions": [
{
"type": "redirect",
"config": {
"format": "ngrok",
"to": "/api/v1/carmen/sandiego/${conn.geo.city}",
"status_code": 301
}
}
]
}
Request:
curl https://example.ngrok.app/api/v1
Result:
< HTTP/2 301
< location: https://example.ngrok.app/api/v1/carmen/sandiego/seattle
Using cel expressions in headers
- YAML
- JSON
# snippet
---
actions:
- type: "redirect"
config:
format: "ngrok"
to: "/api/v1/soon?time=${string(conn.ts.start)}"
status_code: 308
headers:
x-custom-header: "custom-value"
x-template-example: "started at ${conn.ts.start}"
// snippet
{
"actions": [
{
"type": "redirect",
"config": {
"format": "ngrok",
"to": "/api/v1/soon?time=${string(conn.ts.start)}",
"status_code": 308,
"headers": {
"x-custom-header": "custom-value",
"x-template-example": "started at ${conn.ts.start}"
}
}
}
]
}
Request:
curl https://example.ngrok.app/api/v1/foobar
Result:
< HTTP/2 308
< location: https://example.ngrok.app/api/v1/soon?time=2024-06-24T15:30:00Z
< x-custom-header: 2024-06-24T15:30:00Z
< x-template-example: started at 2024-06-24T15:30:00Z