[URL Rewrite] URL Rewrite
This section configures regex-based URL rewriting, HTTP redirection, and request rejection.
URL rewriting executes before traffic routing and upstream connection establishment, supporting internal request URL modification, returning HTTP 302/307 redirects to the client, or rejecting requests directly.
HTTPS traffic must first be decrypted via MITM before rewrite rules can apply.
Syntax Declaration
Each line defines a single URL rewrite rule with whitespace-separated parameters:
[URL Rewrite]
pattern replacement [type]Component breakdown:
pattern: Regular expression matching the full HTTP request URL.replacement: Target URL string, or a placeholder token.[type]: Optional rewrite action type, strictly lowercase (header,302,307,reject). Defaults toheaderinternal rewrite when omitted, and canonical formatting omits the defaultheader.
Quoting and Escaping
[URL Rewrite] uses whitespace-delimited syntax, separating tokens by spaces or tabs. Enclose pattern or replacement in double quotes "..." whenever it:
- Contains spaces, tabs, or double quotes (
"); - Is an empty string (
""); - Starts with
#,;, or[(outside quotes,#or;at line start or after whitespace starts a comment, and[at line start is parsed as a section header); - Equals
%APPEND%or%INSERT%(which would otherwise be parsed as a section-level merge directive at statement start).
Inside double quotes, \" represents a double quote and \\ represents a backslash. A closing quote must be followed by whitespace or end of line, and unquoted tokens must not contain double quotes.
Rewrite Types & Behaviors
header(Default when type is omitted; omitted in canonical formatting) Internal rewrite. Replaces the request URL sent to upstream withreplacement; subsequent rule evaluation and connections operate on the rewritten URL.302(Explicitly specified) Temporary redirect. Returns an HTTP 302 Found response to the client with the rewritten URL in theLocationheader.307(Explicitly specified) Temporary redirect preserving the HTTP request method. Returns an HTTP 307 Temporary Redirect response with the rewritten URL in theLocationheader.reject(Explicitly specified) Rejects the request and terminates the connection. Positional arguments must be maintained, typically using-as a placeholder inreplacement.
Capture Groups & Execution Pipeline
- Regex Capture Group References: The
replacementfield supports referencing captured groups frompatternusing$1,$2, etc., enabling dynamic path and parameter mapping. - First-Match-Wins Principle: Rules evaluate sequentially from top to bottom. Once a request URL matches a rule, the corresponding action executes immediately and subsequent URL rewrite rules are skipped.
- Pipeline Processing: Requests modified via
headerrewrite continue into subsequent Header Rewrite, Response, and Rule pipelines carrying the updated URL.
Configuration Examples
Basic Configuration
[URL Rewrite]
# Temporarily redirect HTTP requests to HTTPS (302)
^http://(www\.)?example\.com/(.*) https://example.com/$2 302
# Internal path rewrite (type omitted, defaults to header)
^https?://api\.example\.com/v1/(.*) https://api.example.com/v2/$1
# Enclose parameters containing spaces or special leading characters in double quotes
"^https?://example\.com/search\?q=hello world$" "https://example.com/search?q=hi%20there" 302
# Temporary redirect preserving original request method (307)
^https?://service\.example\.com/submit$ https://service.example.com/v2/submit 307
# Reject requests matching tracking path (reject)
^https?://tracking\.example\.com/analytics/ - reject