Skip to content

Cloudflare Transform Rules: rewrite URLs and modify request/response headers

Transform Rules cover URL rewrites, request-header changes, and response-header changes. Static vs dynamic expressions, real examples that strip tracking params, force a header, and attach a Permissions-Policy.

A site team needs three things the origin won’t give them: drop utm_* query parameters before they reach the origin so analytics doesn’t double-count, set a X-Forwarded-Host header pointing at the public domain because the upstream is behind a Pages proxy, and attach a Permissions-Policy to every response without a code deploy. Transform Rules cover all three. This is the reference for when to use which kind and what the expressions actually look like.

How to verify

List the Transform Rules already configured on a zone and inspect a request to see which rule modified it. The three Transform Rule phases each live in their own ruleset:

curl -s -H "Authorization: Bearer $CF_API_TOKEN" \
  "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets" \
  | jq '.result[] | select(.phase | test("http_request_transform|http_request_late_transform|http_response_headers_transform")) | {id, phase, name}'

# Fetch a sample URL and inspect what changed
curl -sI "https://example.com/page?utm_source=twitter&utm_campaign=may"

Look at the response headers for cf-ray and any header your rule should have added (e.g. permissions-policy). For URL rewrites, the origin access logs are authoritative — the rule only changes what the origin sees, the client URL bar is untouched.

What’s happening

Transform Rules sit in three distinct phases of the Cloudflare request pipeline. URL Rewrite runs before the request leaves the edge for the origin — it changes the path and query string the origin sees, never the URL in the browser. HTTP Request Header Modification runs in the same phase: add, set, or remove headers the origin will receive. HTTP Response Header Modification runs on the way back, after the origin has responded — add, set, or remove headers the client will receive.

Clientbrowser / APIrequestCloudflare edge1. URL Rewrite2. Request Header Mod3. Response Header Modrewritten requestresponseOriginapp serverSteps 1 and 2 affect the origin’s view. Step 3 affects the client’s view.The browser URL bar is never touched by Transform Rules.
Transform Rules fire at three different points — request rewrite, request headers, response headers.

Expressions in Transform Rules use the same filter language as the WAF and Cache Rules — http.host, http.request.uri.path, http.request.uri.query, http.request.headers["x-foo"]. The target of a transform is what differs: a URL Rewrite changes http.request.uri.path or http.request.uri.query; a request-header rule changes http.request.headers; a response-header rule changes the headers Cloudflare emits to the client. Each rule has a “static” mode (typed string) and a “dynamic” mode (an expression evaluated per request).

The procedure

Three example rules cover the patterns you’ll write 90% of the time.

  1. URL Rewrite — strip tracking parameters before origin sees them. Marketing teams add utm_source, utm_medium, utm_campaign, fbclid, gclid for analytics. The origin doesn’t need them and including them in the cache key fragments cache hits. Create a URL Rewrite rule:

    When: (http.host eq "example.com")
    Then: Rewrite to...
      Path → preserve original
      Query → Dynamic → regex_replace(
        http.request.uri.query,
        "(^|&)(utm_[^=]*|fbclid|gclid)=[^&]*",
        ""
      )

    Pair this with a Cache Rule that excludes the same parameters from the cache key — the rewrite makes the origin happy, the cache key change makes the cache happy.

  2. Request Header Modification — set X-Forwarded-Host for an upstream behind a proxy. When the origin is a SaaS or a Pages project that doesn’t know about the public domain, the upstream sees the wrong Host header. Transform Rules can pin the original:

    When: (http.host eq "example.com" and starts_with(http.request.uri.path, "/api/"))
    Then: Set static header
      Name: X-Forwarded-Host
      Value: example.com

    For dynamic values — say, propagating the original cf-ipcountry to a debugging header — use the dynamic mode:

    Name: X-Origin-Country
    Value (dynamic): ip.geoip.country

    Removing a header looks the same with Remove as the operation and just the header name. A common removal is dropping Cookie for static asset paths so they cache properly.

  3. Response Header Modification — attach a Permissions-Policy without a code deploy. Security teams own headers like Permissions-Policy, Strict-Transport-Security, Content-Security-Policy. Putting them in the edge means the engineering team doesn’t carry that responsibility:

    When: (http.host eq "example.com")
    Then: Set static header
      Name: Permissions-Policy
      Value: camera=(), microphone=(), geolocation=(self), interest-cohort=()

    For a CSP that should differ between the admin path and the marketing site, write two rules with different Then values. Cloudflare evaluates them in priority order and the first match wins; you can also configure later rules to “continue” so multiple headers apply.

Incoming requestURL: /page?utm_source=tw&id=42Host: example.comCookie: session=abc(no X-Forwarded-Host)Response: 200, no security headersAfter Transform RulesURL: /page?id=42(URL Rewrite stripped utm_*)Host: example.comX-Forwarded-Host: example.com(Request Header Mod added)Permissions-Policy: camera=(), …(Response Header Mod attached)Same client URL bar throughout — Transform Rules never visibly redirect.
A single request walked through the three Transform Rule phases.

For sites that need conditional rules — “this CSP only on staging” — use the http.host clause to scope the rule. The same expression language allows compound conditions: http.host eq "example.com" and not starts_with(http.request.uri.path, "/api/").

Common pitfalls

  • URL Rewrites are invisible to the client. The browser URL bar doesn’t change. If a marketing person says “the URL still has utm_source”, they’re looking at their browser; the origin and the cache key are what changed. Use Redirect Rules if the goal is a visible URL change.
  • Dynamic values must produce a string. regex_replace(...) returns the string; a bare http.request.headers["x-foo"] returns an array (because multi-valued headers exist). Wrap with to_string(...) or concat(...) when needed.
  • Setting Host doesn’t change the origin Cloudflare routes to. That’s an Origin Rule — Transform Rules only modify the header the origin sees, not the connection itself.
  • Response Header Mod runs after caching. If you change the response header today, the next cache miss applies the new value; cached responses still serve the old header until they expire. Combine with a cache purge if the change must be immediate.
  • Removing a header doesn’t always work on hop-by-hop fields. Headers like Connection, Transfer-Encoding, and Upgrade are managed by the edge — Transform Rules ignore attempts to override them.

For zones we operate, Transform Rules end up holding the security-header policy and the analytics hygiene layer — places where the rules outlive any single application. Pair this with the Redirect Rules article when you need a visible URL change rather than a silent rewrite. Stack Harbor owns the edge configuration as part of managed operations.