Redirect
Overview
The Redirect policy action enables transforming your URL using regular expressions and redirect through location header.
Example
Use this action config in your Traffic Policy
- YAML
- JSON
# snippet
---
actions:
- type: "redirect"
config:
format: "ngrok"
from: "v0/user/([0-9]+).*"
to: "v1/user?id=$1&port=${conn.server_port}"
status_code: 301
headers:
rewrite: "true"
// snippet
{
"actions": [
{
"type": "redirect",
"config": {
"format": "ngrok",
"from": "v0/user/([0-9]+).*",
"to": "v1/user?id=$1&port=${conn.server_port}",
"status_code": 301,
"headers": {
"rewrite": "true"
}
}
}
]
}
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 request. |
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.
Templating Examples
- YAML
- JSON
# simple variable replacement
---
actions:
- type: "redirect"
config:
format: "ngrok"
to: "/v1/api/carmen/sandiego?found=${conn.geo.city}"
status_code: 301
headers:
rewrite: "true"
// simple variable replacement
{
"actions": [
{
"type": "redirect",
"config": {
"format": "ngrok",
"to": "/v1/api/carmen/sandiego?found=${conn.geo.city}",
"status_code": 301,
"headers": {
"rewrite": "true"
}
}
}
]
}
- YAML
- JSON
# converting variables using cel expressions
---
actions:
- type: "redirect"
config:
format: "ngrok"
to: "/v1/api/soon?time=${string(conn.start_ts)}"
status_code: 301
headers:
rewrite: "true"
// converting variables using cel expressions
{
"actions": [
{
"type": "redirect",
"config": {
"format": "ngrok",
"to": "/v1/api/soon?time=${string(conn.start_ts)}",
"status_code": 301,
"headers": {
"rewrite": "true"
}
}
}
]
}
- YAML
- JSON
# combining variables and regex capture groups
---
actions:
- type: "redirect"
config:
format: "ngrok"
to: "/v1/api?lat=$1&long=${conn.geo.longitude}"
status_code: 301
headers:
rewrite: "true"
// combining variables and regex capture groups
{
"actions": [
{
"type": "redirect",
"config": {
"format": "ngrok",
"to": "/v1/api?lat=$1&long=${conn.geo.longitude}",
"status_code": 301,
"headers": {
"rewrite": "true"
}
}
}
]
}