Rewrite (Beta)
For AI agents: this page in Markdown · site index (llms.txt)
Rewrite automatically changes the traffic going through the proxy. Unlike Map Local, the request still goes to the server. It lets you add a header, send a request to another server, turn a 200 into a 503, or replace a piece of JSON in a body.
The window opens from Tools → Rewrite (Beta)… (Cmd + Option + R on macOS, Ctrl + Shift + W on Windows and Linux).
Rule sets
A set is a list of locations and an ordered list of rules. A set matches a request or response when at least one of its enabled locations does, and its enabled rules are then applied in order, top to bottom. Sets apply in the order they stand in the list on the left.
The Rewrite switch in the window turns the whole feature on and off at once. With Rewrite off, the rules are kept but not applied.
Locations
A location describes which requests a set applies to. The fields:
| Field | What it sets |
|---|---|
| Protocol | http, https, ws, wss or any |
| Host | the host name, with no scheme, port or path |
| Port | the port number; empty means the protocol's default |
| Path | the path, for example /v1/* |
| Query | the query string after ?, for example page=1 |
An empty field means any value. Text fields take * for any characters, ? for one character, [a-z] and [!a-z] for a character in a range or outside it, and \ to escape the next character. Case does not matter.
A few details:
*in a host does not cross:or/, while*in a path crosses/:/v1/*also matches/v1/users/42.- The query is matched anywhere in the query string.
- A WebSocket upgrade belongs to the
wsorwssprotocol, not tohttporhttps. - A set with no locations applies to every request. A set whose locations are all turned off does nothing.
CONNECTtunnels are never rewritten.
Rules
| Where | Rule types |
|---|---|
| Request | Add Header, Modify Header, Remove Header, Host, Path, URL, Add Query Param, Modify Query Param, Remove Query Param, Body |
| Response | Add Header, Modify Header, Remove Header, Response Status, Body |
A rule has Match and Replace with fields; header and query parameter rules have them separately for the name and the value. How they work:
- An empty match field matches anything.
- A header or parameter name is compared whole, header names case-insensitively. A value is searched as a substring.
- With Regex ticked the match is a regular expression, and the replacement can refer to its groups:
$1or${name}. - Replace: First replaces the first occurrence only, All replaces every one.
- An empty replacement name keeps the name as it was.
- An empty match value with a non-empty replacement replaces the whole value. If both are empty, the value is kept.
- A match value with an empty replacement cuts out what was found.
- Remove Header and Remove Query Param need a name or a value to match; Add Query Param needs a name.
- The status can be replaced with any code from 200 to 599.
A rule is edited as a draft: changes apply after Save, and Revert brings back the saved version. A saved rule applies to new requests.
What it looks like in practice
Add a header to every API request. Location: protocol https, host api.example.com. Rule: Request, Add Header, name X-Debug, value 1.
Send requests to a staging server. Location: host api.example.com. Rule: Host, match api.example.com, replace with staging.example.com. The request goes to the other server, and the Host header changes along with the address.
Test error handling. Location: host api.example.com, path /v1/orders. Rule: Response, Response Status, replace with 503. The response body is still the server's.
Change a field in JSON. Rule: Response, Body, match "premium":false, replace with "premium":true.
Behaviour
- A request is matched against the locations as the client sent it. The captured traffic shows what went to the server and what reached the client after rewriting.
- When a rule changes the host or the URL, the request goes to the new address, and the
Hostheader changes with it. A rule that would send the request to the proxy itself is skipped. - Service headers that Solpuga sets cannot be changed; such a rule will not save.
- An HTTPS request is rewritten only if its host is decrypted. When a set is saved, the hosts in its enabled locations for
https,wssor any protocol are added to the SSL Proxying list automatically: the exact name or a mask like*.example.com, with the port if it is not 443. A host on the bypass list is never decrypted, so rules do not apply to it.
Bodies
A body rule reads the body only if its size is known in advance and is at most 16 MB. A body in gzip, br or another encoding is decoded first. If a rule changed something, the body goes out decoded with a new Content-Length; if not, it goes out as it came.
Body rules do not apply to SSE streams, responses to HEAD, 1xx, 204 and 304 responses, or 206 partial responses.
Import and export
The Export… button saves every set to an XML file; Export… in a set's menu saves that set only.
Import… takes Solpuga files and Rewrite rule files from Charles (in Charles: Tools → Import/Export Settings → the Export tab → Rewrite). The file can be up to 16 MB. Imported sets are added at the end of the list and keep their on or off state. Whatever cannot be carried over is skipped, and Solpuga says how much and what was skipped. Unlike saving a set, an import does not add hosts to the SSL Proxying list: add them yourself under Settings → SSL Proxying.
Carrying rules over from Charles is covered in full in Migrating from Charles.
Rules are stored in an encrypted local database.