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 to your upstream

Example

Traffic Policy for rewriting an incoming url's version and id path params.

# 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
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.

More Examples

Simple variable replacement
# 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

# 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

# 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.

NameTypeDescription
actions.ngrok.url_rewrite.matches[]stringThe list of matched elements.
actions.ngrok.url_rewrite.urlstringThe resulting URL after it was re-written.
actions.ngrok.url_rewrite.error.codestringCode for an error that occurred during the invocation of an action.
actions.ngrok.url_rewrite.error.messagestringMessage for an error that occurred during the invocation of an action.