URL Rewrite
Overview
The URL Rewrite action allows you to transform the URL path and query parameters of the incoming request.
Example
Use this action config in your Traffic Policy
- YAML
- JSON
# snippet
---
actions:
- type: "url-rewrite"
config:
format: "ngrok"
from: "v0/user/([0-9]+).*&filter=(.*)"
to: "v1/user?id=$1&filter=${req.query_params['filter'][0]}"
// snippet
{
"actions": [
{
"type": "url-rewrite",
"config": {
"format": "ngrok",
"from": "v0/user/([0-9]+).*&filter=(.*)",
"to": "v1/user?id=$1&filter=${req.query_params['filter'][0]}"
}
}
]
}
Behavior
When executed, this action will replace all regex matches with the configured replacement. Before regex replacement, all cel expressions will be interpolated and replaced with their corresponding values.
Reference
Supported Directions
- Inbound
Configuration
Type |
---|
url-rewrite |
Parameter | Description | |
---|---|---|
from | string | A regular expression string to match inside of the URL. Also contains cel expressions outside of regular regex. |
to | string | A regular expression string to replace the from match with. Also contains special cel expressions outside of regular regex. |
format | string | (default: ngrok ). Determines interpolation format in to /from fields. |
Examples
For an exhaustive list of variables, macros, and expressions, checkout cel expressions.
- YAML
- JSON
# snippet
---
format: "ngrok"
to: "/v0/api/carmen/sandiego?city=${conn.geo.city}"
from: "/v1/api/carmen/sandiego?found=${conn.geo.city}"
// snippet
{
"format": "ngrok",
"to": "/v0/api/carmen/sandiego?city=${conn.geo.city}",
"from": "/v1/api/carmen/sandiego?found=${conn.geo.city}"
}
- YAML
- JSON
# snippet
---
format: "ngrok"
to: "/v0/api/when-will-then-be-now?time=${string(conn.start_ts)}"
from: "/v1/api/soon?time=${string(conn.start_ts)}"
// snippet
{
"format": "ngrok",
"to": "/v0/api/when-will-then-be-now?time=${string(conn.start_ts)}",
"from": "/v1/api/soon?time=${string(conn.start_ts)}"
}
- YAML
- JSON
# snippet
---
format: "ngrok"
to: "/v0/api?lat=(${conn.geo.latitude})"
from: "/v1/api?lat=$1&long=${conn.geo.longitude}"
// snippet
{
"format": "ngrok",
"to": "/v0/api?lat=(${conn.geo.latitude})",
"from": "/v1/api?lat=$1&long=${conn.geo.longitude}"
}