URL Rewrite
Overview
The URL Rewrite action allows you to transform the URL path and query parameters of the incoming request to your upstream
Example
Traffic Policy for rewriting an incoming url's version
and id
path params.
- YAML
- JSON
# snippet
---
actions:
- type: "url-rewrite"
config:
format: "ngrok"
from: "/v1/users/([0-9]+)"
to: "/v2/users?id=$1"
// snippet
{
"actions": [
{
"type": "url-rewrite",
"config": {
"format": "ngrok",
"from": "/v1/users/([0-9]+)",
"to": "/v2/users?id=$1"
}
}
]
}
Request:
curl https://example.ngrok.app/api/v1/users/42
Result:
- Url rewritten on incoming request
> GET https://example.ngrok.app/api/v2/users?id=42 HTTP/1.1
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. Supports interpolating the results of cel expressions outside of regex. |
to | string | A regular expression string to replace the from match with. Supports interpolating the results of cel expressions outside of regex. |
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
Simple variable replacement
- YAML
- JSON
# simple variable replacement
---
actions:
- type: "url-rewrite"
config:
format: "ngrok"
from: "/api/v1/api?lat=(${conn.geo.latitude})"
to: "/api/v2/api?lat=$1&long=${conn.geo.longitude}"
// simple variable replacement
{
"actions": [
{
"type": "url-rewrite",
"config": {
"format": "ngrok",
"from": "/api/v1/api?lat=(${conn.geo.latitude})",
"to": "/api/v2/api?lat=$1&long=${conn.geo.longitude}"
}
}
]
}
Request:
curl https://example.ngrok.app/api/v1/users/42
Result:
- Url Rewritten* on incoming _request*
# Url on incoming request to upstream
> GET https://example.ngrok.app/api/v2/users?id=42 HTTP/1.1
- YAML
- JSON
# regex and cel expressions
---
actions:
- type: "url-rewrite"
config:
format: "ngrok"
from: "/api/v1/api/(carmen)/sandiego?city=${conn.geo.city}"
to: "/api/v2/api/$1/sandiego?found=${conn.geo.city}"
// regex and cel expressions
{
"actions": [
{
"type": "url-rewrite",
"config": {
"format": "ngrok",
"from": "/api/v1/api/(carmen)/sandiego?city=${conn.geo.city}",
"to": "/api/v2/api/$1/sandiego?found=${conn.geo.city}"
}
}
]
}
Request:
curl https://example.ngrok.app/api/v1/users/42
Result:
- Url rewritten on incoming request
# New Url on incoming request to upstream
> GET https://example.ngrok.app/api/v2/users?id=42 HTTP/1.1
- YAML
- JSON
# variable replacement with comparisons on partial path
---
actions:
- type: "url-rewrite"
config:
format: "ngrok"
from: "?not-now=${conn.ts.start > time.now}"
to: "?now=${conn.ts.start < time.now}"
// variable replacement with comparisons on partial path
{
"actions": [
{
"type": "url-rewrite",
"config": {
"format": "ngrok",
"from": "?not-now=${conn.ts.start > time.now}",
"to": "?now=${conn.ts.start < time.now}"
}
}
]
}
Request:
curl https://example.ngrok.app/api/v1/users/42
Result:
- Url Rewritten on incoming request
> GET https://example.ngrok.app/api/v2/users?id=42 HTTP/1.1
Action Result Variables
The following variables are available following the invocation of this action.
Name | Type | Description |
---|---|---|
actions.ngrok.url_rewrite.matches | []string | The list of matched elements. |
actions.ngrok.url_rewrite.url | string | The resulting URL after it was re-written. |
actions.ngrok.url_rewrite.error.code | string | Code for an error that occurred during the invocation of an action. |
actions.ngrok.url_rewrite.error.message | string | Message for an error that occurred during the invocation of an action. |