Skip to main content
The rewrite module is a middleware which can mutate HTTP requests. It can change the request method, URI (path and query), and perform various transformations on the request before it continues down the handler chain. Module ID: 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.)
It is atypical to combine setters and modifiers in a single rewrite.
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

string
Changes the request’s HTTP verb.

URI

string
Changes the request’s URI, which consists of path and query string. Only components specified will be changed.
  • /foo.html or foo.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
Supports placeholders. To preserve existing query string:
Key-value pairs added to query string will not overwrite existing values (append-only).

Path Modifications

string
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.
string
Strips the given suffix from the end of the URI path.Like strip_path_prefix, should be written in normalized form.

Substring Replacement

array
Performs substring replacements on the URI.
string
The substring to find. Supports placeholders.
string
The substring to replace with. Supports placeholders.
integer
Maximum number of replacements per string. Set to ≤ 0 for no limit (default).

Regular Expression Replacement

array
Performs regular expression replacements on the URI path.
string
required
The regular expression to find (RE2 syntax).
string
The substring to replace with. Supports placeholders and regex capture groups.

Query Operations

object
Mutates the query string of the URI.
array
Renames a query key from key to val, without affecting the value.
array
Sets query parameters, overwriting existing values.
array
Adds query parameters. Does not overwrite existing fields, only appends additional values.
array
Replaces query parameter values using substring or regex matching.
string
The key to replace. Use * to replace in all keys.
The substring to search for.
string
The regular expression to search with.
string
The string with which to replace matches.
array of strings
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
Exception: If the pattern contains // (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

This replaces all underscores with hyphens in both path and query.

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%2fDC whereas /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.