URL Rewriting
The URL rewriting feature supports two modes:
- Redirect mode: Returns an HTTP 3xx redirect response. The client is aware of the redirect and initiates a new request.
- Header mode: Directly modifies the request URI and Host header, transparently forwarding the request to the new address without the client's awareness.
Rewriting the URL of HTTPS requests requires configuring MITM and installing a CA certificate. Below is a detailed explanation of each field:
-
match (string), required
A regular expression for URL matching, which matches the full request URL (including protocol, hostname, path, and query parameters). Capture groups are supported.
-
location (string), required
The redirect target URL. Supports referencing capture groups with
$1,$2, etc. -
status_code (integer), optional
The HTTP redirect status code (
301permanent redirect,302temporary redirect,307temporary redirect preserving method,308permanent redirect preserving method). When omitted, header mode is used. -
disabled (bool), optional
Whether to disable this rule.
Configuration Example
url_rewrites:
# Redirect mode: returns a 307 redirect response
- match: "(.*google)\\.cn"
location: $1.com
status_code: 307
# Redirect mode: returns a 301 permanent redirect
- match: "^https://old\\.example\\.com/(.*)$"
location: "https://new.example.com/$1"
status_code: 301
# Header mode: transparent forwarding, client unaware
- match: "^https://api\\.example\\.com/v1/(.*)$"
location: "https://api.example.com/v2/$1"
In the first example, the system will find all requests whose URL matches (.*google)\.cn and return a 307 redirect response to redirect them to the corresponding .com domain. In the third example, the system transparently forwards matching requests to the v2 API without the client being aware of the address change.