HTTP Rewrite
HTTP rewrite rules match URLs with regular expressions. You can rewrite a URL, redirect a request, or reject it, for example to block advertising and tracking requests.
http:
# Rewrite the URL transparently, return a redirect, or reject the request.
url-rewrite:
- ^http://g\.cn https://www.google.com transparent
- ^https?://www\.google\.cn https://www.google.com 302 # Directly returns a 302 redirect response
- ^https?://ad\.example - reject # Rejects the request
header-rewrite:
- ^http://g\.cn request-add DNT 1
- ^http://g\.cn request-del DNT
- ^http://g\.cn request-replace DNT 1
- ^http://g\.cn request-replace-regex User-Agent Go-http-client curl
- ^http://g\.cn response-add DNT 1
- ^http://g\.cn response-del DNT
- ^http://g\.cn response-replace DNT 1
- ^http://g\.cn response-replace-regex User-Agent Go-http-client curl
body-rewrite:
- '^https://api\.example\.com/ response-jq del(.debug) | .name = "Stash"'
mock:
- match: ^https?://ad\.example
status-code: 503URL Rewrite
For transparent, 302, and 307, the target URL can include text captured by the regular expression. Use $1 or ${1} for the first capture group, $0 for the full match, and $$ for a literal dollar sign. Use braces when a digit follows a group reference. For example, ${1}0 inserts the first capture group followed by a zero. Header and body rewrites use the same replacement syntax.
transparent
Stash rewrites the request URL internally and forwards the request to the new HTTP or HTTPS URL.
302 / 307
The HTTP Engine returns the specified status code and sets the Location response header to the target URL.
reject
Rejects the request, with the same effect as a rule that matches REJECT.
reject-200
Returns a 200 response with an empty response body.
reject-img
Returns a 200 response with a 1×1 GIF as the response body.
reject-dict
Returns a 200 response with a response body of {}.
reject-array
Returns a 200 response with a response body of [].
HTTP Header Rewrite
Header rewrite rules can add, delete, or replace HTTP request or response headers.
request-add / response-add
Appends a header to the request or response.
request-del / response-del
Removes the named header from the request or response.
request-replace / response-replace
Replaces the value of an existing request or response header.
request-replace-regex / response-replace-regex
Replaces matching text in a request or response header value using a regular expression. The replacement follows the syntax described in URL Rewrite.
HTTP Body Rewrite
iOS/tvOS2.8+macOS2.8+Body rewrite rules match a request URL with a regular expression and modify the request or response body. Each rule contains the URL pattern, followed by an action and its expression.
http:
body-rewrite:
- '^https://api\.example\.com/ request-replace-regex old new'
- '^https://api\.example\.com/ response-jq del(.debug) | .name = "Stash"'
- '^https://api\.example\.com/ response-json-replace data.name "Stash"'
- '^https://api\.example\.com/ response-json-add meta.enabled true'
- '^https://api\.example\.com/ response-json-del debug internal.trace'Actions beginning with request- modify the request body, while actions beginning with response- modify the response body. Rules run in configuration order, so later rules can continue processing the body produced by earlier rules.
Stash handles compressed bodies automatically, so you don’t need to decompress them in your rules.
Regular Expression Replacement
request-replace-regex and response-replace-regex take one or more regular-expression replacement pairs and apply them to UTF-8 text in the order written. Each regular expression replaces all its matches. The replacement follows the syntax described in URL Rewrite.
http:
body-rewrite:
- '^https://api\.example\.com/ response-replace-regex old new disabled enabled'This rule first replaces every old with new, then replaces every disabled with enabled.
jq
request-jq and response-jq use the body as JSON input and generate a new JSON body with a jq expression. Expressions can combine assignment, deletion, filtering, and pipeline operations.
http:
body-rewrite:
- '^https://api\.example\.com/ request-jq .client = "Stash"'
- '^https://api\.example\.com/ response-jq del(.debug) | .items |= map(select(.enabled))'JSON Add, Delete, and Replace
The JSON actions are shortcuts for common jq operations. Paths omit the leading ., and can address array elements such as items[0].name. Values use jq/JSON literals and must be written as a single argument without spaces.
| Action | Expression | Behavior |
|---|---|---|
request-json-replace / response-json-replace | path value [path value ...] | Replaces the value when the current path value is neither false nor null |
request-json-add / response-json-add | path value [path value ...] | Writes the value when the path is missing or null, preserving an existing non-null value |
request-json-del / response-json-del | path [path ...] | Deletes the path when its current value is neither false nor null |
A single rule can process multiple paths in sequence. For example:
http:
body-rewrite:
- '^https://api\.example\.com/ response-json-replace data.name "Stash" data.enabled true'
- '^https://api\.example\.com/ response-json-add meta.source "proxy" meta.version 1'
- '^https://api\.example\.com/ response-json-del debug internal.trace'Mock
A mock rule returns a static response. To generate a response dynamically, use JavaScript.
http:
mock:
- match: ^https?://example.stash\.ws/json
text: '{}'
status-code: 200
headers:
Content-Type: application/json
- match: ^https?://example.stash\.ws/base64
base64: 'CgVUZXJyeRAeGhF0ZXJyeUBleGFtcGxlLmNvbQ=='
status-code: 200
headers:
Content-Type: application/x-protobufmatch: Matched regular expression.status-code: HTTP status code to return, defaults to 200 if not specified.headers: HTTP response headers to return, no need to setContent-Length.
Response content:
text: Returned text content encoded in UTF-8.base64: Returned content as binary encoded in Base64.
Rewrite HTTP with JavaScript
For the request and response objects available to scripts and how to return changes, see Rewrite HTTP.