http.handlers.rewrite
Overview
The rewrite handler provides two types of operations:- Setters - Completely overwrite values (method, URI)
- Modifiers - Modify existing values in a differentiable way (strip prefix/suffix, substring replacement, etc.)
To ensure consistent behavior, prefix and suffix stripping is performed in URL-decoded (unescaped, normalized) space by default, except for specific bytes where an escape sequence is used in the pattern.
Configuration
Method
Changes the request’s HTTP verb.
URI
Changes the request’s URI, which consists of path and query string. Only components specified will be changed.Key-value pairs added to query string will not overwrite existing values (append-only).
/foo.htmlorfoo.html- Only changes path, preserves query?a=b- Only changes query, preserves path/foo?a=b- Changes both path and query?- Clears the query string
Path Modifications
Strips the given prefix from the beginning of the URI path.The prefix should be written in normalized (unescaped) form, but if an escaping (
%xx) is used, the path will be required to have that same escape at that position.Strips the given suffix from the end of the URI path.Like
strip_path_prefix, should be written in normalized form.Substring Replacement
Regular Expression Replacement
Query Operations
Mutates the query string of the URI.
Renames a query key from
key to val, without affecting the value.Sets query parameters, overwriting existing values.
Adds query parameters. Does not overwrite existing fields, only appends additional values.
Deletes query parameters by name.
Path Cleaning
For all modifiers, paths are cleaned before being modified:- Multiple consecutive slashes are collapsed into a single slash
- Dot elements (
.and..) are resolved and removed
// (repeated slashes), slashes will not be merged while cleaning so that the rewrite can be interpreted literally.
Configuration Examples
Simple URI Rewrite
Strip Path Prefix
Strip Path Suffix
Path Regexp Replacement
Query String Manipulation
Change Method
Complex Example: API Versioning
Substring Replacement
Use Cases
SPA Fallback
Clean URLs (Remove .html)
API Gateway Path Routing
Normalize Query Parameters
Important Notes
Path vs Query Matching:
- Escape sequences in patterns are compared with the request’s raw/escaped path for those bytes only
- The special escaped wildcard
%*can be used for spans that should NOT be decoded /bands/%*matches/bands/AC%2fDCwhereas/bands/*does not
Query String Handling:
- Query keys can appear multiple times
- Operations respect the multi-value nature of query strings
- Set operations replace all values for a key
- Add operations append to existing values
Rewrite Execution:
Rewrites are applied immediately when the handler executes. The changes affect subsequent handlers in the chain but do not affect matchers that have already been evaluated.