[Header Rewrite] Header Rewrite
This section modifies HTTP request headers before they are sent upstream, supporting additions, deletions, replacements, and regular expression substitutions.
HTTPS traffic must first be decrypted via MITM before request headers can be inspected and modified.
Syntax Declaration
Each line defines a single header rewrite rule with whitespace-separated parameters:
ini
[Header Rewrite]
[http-request] pattern action header_name [arguments...]Component breakdown:
[http-request]: Optional trigger direction prefix specifying that the rule applies to the client request phase. It may be omitted, and canonical formatting omits it by default.http-responseis not supported (specifyinghttp-responsefails validation; to modify response headers, use atype=http-responsescript in Script).pattern: Regular expression matching the full request URL.action: Header manipulation directive specifying the action to perform on the target header.header_name: Target HTTP header field name (ASCII case-insensitive).[arguments...]: Arguments required by the action directive (such as new field values, or a regular expression and its replacement).
Actions & Arguments
header-add/add(Argument:<value>) Appends the specified header field and value to the request headers. If a header with the same name already exists, the original header is preserved and the new value is appended.header-del/del/delete(No additional arguments) Removes all occurrences ofheader_namefrom request headers.header-replace/replace(Argument:<value>) Replaces the value of an existing header field with<value>. If the header does not exist, the request remains unchanged without creating a new header.header-replace-regex/replace-regex(Arguments:<regex> <replacement>or"<regex> <replacement>") Performs regular expression matching and substitution on the value of an existing header field. If the header does not exist, the request remains unchanged. If<regex>contains spaces, enclose it in double quotes (otherwise the fourth token is parsed as<regex>and remaining tokens are joined into<replacement>); alternatively, both can be passed as a single quoted argument"<regex> <replacement>"split on the first space.
Quoting and Escaping
[Header Rewrite] uses whitespace-delimited syntax, separating tokens by spaces or tabs. Enclose parameters containing spaces or special characters in double quotes "...":
- Double quotes are required whenever a token contains
", is an empty string"", starts with#,;, or[, equals%APPEND%or%INSERT%(parsed as section-level merge directives at statement start), or contains#or;preceded by whitespace outside quotes (which starts an inline trailing comment). - For
header-addandheader-replace, multiple unquoted trailing tokens are joined with a single space; enclose the value in double quotes whenever it contains consecutive spaces, tabs, comment triggers, or quotes. - Inside double quotes,
\"represents a double quote and\\represents a backslash; closing quotes must be followed by whitespace or end of line.
Processing Pipeline & Constraints
- Host Header Constraint: The
Hostheader is determined by the request URL and cannot be modified directly in this section. To modify the target host, use URL Rewrite. - Rule Execution Order: Multiple rules are applied sequentially from top to bottom. Subsequent rules operate on headers modified by preceding rules.
- Pipeline Order: Header rewriting occurs after URL Rewrite and before rule routing and external connection establishment.
Configuration Examples
Basic Configuration
ini
[Header Rewrite]
# Remove Cookie header for tracking domain (optional http-request prefix omitted)
^https?://tracking\.example\.com/ header-del Cookie
# Replace client User-Agent identifier (quoted value with spaces)
^https?://.*\.example\.com/ header-replace User-Agent "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36"
# Add Authorization header (optional http-request prefix shown)
http-request ^https?://api\.internal\.example\.com/ header-add Authorization "Bearer <TOKEN>"
# Regex substitution on Referer header
^https?://.*\.example\.com/api/ header-replace-regex Referer "https?://[^/]+/" https://example.com/
# Regex substitution using combined quoted form "<regex> <replacement>"
^https?://api\.example\.com/ header-replace-regex X-Client "^LegacyClient/([0-9.]+) NewClient/$1"