Skip to main content

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

# 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
fromstringA regular expression string to match inside of the URL. Supports interpolating the results of cel expressions outside of regular regex.
tostringA regular expression string to replace the from match with. Supports interpolating the results of cel expressions outside of regular regex.
status_codeint3xx status code used for redirecting, 302 by default.
headersMap<string, string>Headers to be added to the request.
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: "redirect"
config:
format: "ngrok"
to: "/v1/api/carmen/sandiego?found=${conn.geo.city}"
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"
# 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"