Skip to main content

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

# 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
fromstringA regular expression string to match inside of the URL. Supports interpolating the results of cel expressions outside of regex.
tostringA regular expression string to replace the from match with. Supports interpolating the results of cel expressions outside of regex.
formatstring(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

# simple variable replacement
---
actions:
- type: "url-rewrite"
config:
format: "ngrok"
from: "/v0/api?lat=(${conn.geo.latitude})"
to: "/v1/api?lat=$1&long=${conn.geo.longitude}"
# regex and variable replacement
---
actions:
- type: "url-rewrite"
config:
format: "ngrok"
from: "/v0/api/(carmen)/sandiego?city=${conn.geo.city}"
to: "/v1/api/$1/sandiego?found=${conn.geo.city}"
# variable replacement with comparisons
---
actions:
- type: "url-rewrite"
config:
format: "ngrok"
from: "/v0/api/when-will-then-be-now?not-now=${conn.start_ts > time.now}"
to: "/v1/api/soon?now=${conn.start_ts < time.now}"