# Match and replace

Match and replace rules rewrite traffic as it passes through the proxy.
Use them to add a header to every request, swap a user agent, turn a
feature flag on in a response, or point one path at another while you
test. Rules work whether intercept is on or off, and nothing is held: a
rewritten message goes on at once.

Open **Proxy › Settings › Match and replace**.

## How a rule reads

Each rule is one sentence: *In requests (or responses), rewrite the
request line (status line), headers or body: find this, replace it with
that.*

| Part | What the rule sees |
| --- | --- |
| Request line | `METHOD URL VERSION`, with the full URL: `GET https://example.com/api HTTP/1.1` |
| Status line | `VERSION STATUS REASON`: `HTTP/1.1 403 Forbidden` |
| Headers | Each header line on its own: `User-Agent: Mozilla/5.0` |
| Body | The body bytes as they travel |

- **Find** is plain text by default and replaces every occurrence,
  case-sensitively. Turn on **regex** to use a regular expression; the
  replacement can then use groups (`\1`, `\g<name>`).
- **Replace** may be empty, which removes what was found. A header line
  replaced by nothing is removed.
- A header rule with an empty **Find** adds its replacement as a new
  header: Find empty, Replace `X-Debug: 1`.
- **in scope** limits a rule to hosts in the capture vault's scope.
- Rules run top to bottom, each on the result of the one before.
  Right-click a rule (or focus it and press Delete) to switch it off,
  duplicate it, move it or remove it.

Nothing changes until you press **Save rules**.

## Try before you save

Under the list, **Try the rules on a message** runs the rules you are
editing, saved or not, on a message you paste as raw HTTP: a request
(`GET /path HTTP/1.1` with its headers) or a response (`HTTP/1.1 200 OK`
with its headers), then a blank line and the body. The result shows the
message as it would be sent.

## What to keep in mind

- **Compressed bodies.** Bodies are rewritten as they travel. A response
  sent with `Content-Encoding: gzip` holds compressed bytes, so text
  inside it does not match. Remove `Accept-Encoding` from requests (a
  header rule with Find `^Accept-Encoding: .*$`, regex on, Replace empty)
  to get uncompressed responses.
- **Large bodies.** A body is rewritten only when it fits in the proxy
  body limit (512 KiB by default, set in Capture). A larger body goes on
  unchanged, while its headers are still rewritten.
- **Length.** When a body changes, Phantom sets `Content-Length` again
  (or sends the body chunked), so the other side reads it correctly.
- **History.** HTTP history stores what was sent, and marks the exchange
  as edited.
- **Intercept.** When intercept holds a message, you see it already
  rewritten. Forwarding it unchanged sends the rewritten version; your
  own edits replace it.
- **Slow patterns.** Each regular-expression replacement stops after
  100 ms. A rule that runs out of time leaves that message as it was and
  is logged once.

## Who can change the rules

Rules rewrite everything your browser sends and receives, so only you
can save them. Agents can read the rules (`match_replace_get`) and try
rules on a sample message (`match_replace_test`), but cannot change
them.

## API

| Method | Path | Does |
| --- | --- | --- |
| `GET` | `/api/v1/proxy/match-replace` | The saved rules, in order |
| `PUT` | `/api/v1/proxy/match-replace` | Replace the whole list (user only) |
| `POST` | `/api/v1/proxy/match-replace/test` | Run rules on a sample; nothing is saved |

A rule is `{enabled, target: "request"|"response", part:
"first_line"|"header"|"body", match, replace, regex, in_scope_only,
comment}`; `match` and `replace` hold up to 512 characters, and up to 100
rules are kept. A refused list names the rule: `details: {index, field}`.

Code: `backend/app/core/proxy/rewrite.py` (the rules),
`backend/app/runtime/rewrite.py` (storage),
`backend/app/services/rewrite_service.py`,
`backend/app/api/v1/match_replace.py`,
`backend/app/mcp/tools/match_replace.py`.
