HTTP Header Transform Rules: Request Modifications, Response Operations, and Limits
How edge HTTP header transform rules change request and response headers, including operations, expressions, API setup, restrictions, and verification.
This page summarises Cloudflare documentation on Transform Rules that change HTTP request headers sent to your origin and HTTP response headers sent to visitors. Related one-click options are Managed Transforms; for identity-aware request headers on Zero Trust traffic, see Gateway HTTP policies (a separate control plane).
Transform Rules: what changes request vs response headers
Section titled “Transform Rules: what changes request vs response headers”Transform Rules adjust the URI path, query string, and HTTP headers on the Cloudflare network. Header work splits into two rule types:
Use Request Header Transform Rules to manipulate the headers of HTTP requests sent to your origin server (the server where your website or application is hosted).
— Cloudflare, Request Header Transform Rules
Use Response Header Transform Rules to manipulate the headers of HTTP responses sent to website visitors.
— Cloudflare, Response Header Transform Rules
Request rules run in the http_request_late_transform phase; response rules run in http_response_headers_transform. Both require proxied DNS for the zone. More complex logic may use Snippets or Workers instead.
| Direction | Who sees the change | Typical goals |
|---|---|---|
| Request header transform | Origin server | Pass bot scores, strip client headers, tag Workers subrequests |
| Response header transform | Browser / API clients | CORS, security headers, licensing on blocked bots |
Request Header Transform Rules: set, dynamic expressions, and remove
Section titled “Request Header Transform Rules: set, dynamic expressions, and remove”Through Request Header Transform Rules you can set a header to a static string, set it from an expression, or remove a header.
Dashboard operations (create in dashboard):
- Set static — overrides an existing same-named header or adds a new one.
- Set dynamic — value comes from the Rules language expression.
- Remove — drops the header if present; removes all headers with that name.
One rule can modify up to 30 request headers. Rules run in order; later rules can overwrite earlier changes.
Common request header transform examples
Section titled “Common request header transform examples”Static marker to the origin (example):
- Expression:
starts_with(http.request.uri.path, "/en/") - Operation: Set static
- Header:
X-Source, value:Cloudflare
Bot score on the request (example):
- Operation: Set dynamic
- Header:
X-Bot-Score - Value:
to_string(cf.bot_management.score)
Workers subrequest from another zone (example):
(cf.worker.upstream_zone != "" and cf.worker.upstream_zone != "myappexample.com")Set static header X-External-Workers-Subrequest to 1. The field cf.worker.upstream_zone is empty when the request is not a Workers subrequest.
Fraud or JWT data — expressions can use fields such as cf.fraud.email_risk or, after API Shield JWT validation, http.request.jwt.claims (fields reference, JWT transform example). Account Abuse Protection notes that fraud detection data can be passed to the origin via Request Header Transform Rules.
Response Header Transform Rules: set, add, and remove
Section titled “Response Header Transform Rules: set, add, and remove”Response Header Transform Rules support set, add, and remove. The distinction between set and add matters when multiple values for the same header name must coexist.
To set an HTTP response header, overwriting any headers with the same name, use the following parameters…
operation: setTo add an HTTP response header, keeping any existing headers with the same name, use…
operation: add— Cloudflare, Response header API parameter reference
Dashboard labels map to Set static/dynamic (replace same name) and Add static/dynamic (append without removing existing same-named headers) (create response rule).
Response examples people copy often
Section titled “Response examples people copy often”| Task | Operation | Notes |
|---|---|---|
| Wildcard CORS | Set static Access-Control-Allow-Origin: * |
CORS example |
Extra Set-Cookie |
Add static/dynamic | Add response header — Add keeps existing Set-Cookie; Set would replace them |
| Bot score to client | Set dynamic X-Bot-Score |
Set response bot score |
| AI licensing on 403 | Set static Link header |
AI Crawl Control with Transform Rules |
Response expressions use request-side context for many fields (for example visitor country via ip.src.country), not the origin’s location (Response Header Transform Rules remarks).
Create rules in the dashboard or Rulesets API
Section titled “Create rules in the dashboard or Rulesets API”Dashboard workflow
Section titled “Dashboard workflow”- In the Cloudflare dashboard, open Rules Overview and choose Create rule → Request Header Transform Rule or Response Header Transform Rule.
- Name the rule and define When incoming requests match (all traffic or a filter expression).
- Under header modification, pick the operation (set/add/remove as applicable) and enter header name and value or expression.
- Optionally use Set new header for additional headers (up to 30 per rule).
- Deploy the rule or Save as Draft.
See the Transform Rules examples gallery for ready-made patterns.
API workflow
Section titled “API workflow”Request rules via API (create request rule via API):
- Action:
rewrite - Phase:
http_request_late_transform(zone ruleset,kind: zone) - Headers in
action_parameters.headerswithoperation: setorremove, usingvalueorexpression(request API parameters)
Response rules (create response rule via API):
- Phase:
http_response_headers_transform - Operations:
set,add, orremove(response API parameters)
Token permissions include Transform Rules > Edit and Account Rulesets > Read. Terraform examples mirror the same cloudflare_ruleset phases.
Expression fields and header name format
Section titled “Expression fields and header name format”Dynamic values draw from the request fields and functions or response fields and functions lists, including cf.bot_management.*, http.request.uri.*, ip.src.*, cf.worker.upstream_zone, WAF and fraud fields, and on responses http.response.code and http.response.headers.*.
Important expression habits from the docs:
- Read request headers with lowercase names:
http.request.headers["accept-encoding"][0]. - Convert non-strings with
to_string(), e.g.to_string(cf.bot_management.score). - Match low-bandwidth clients with
cf.edge.l4.delivery_rate(see changelog example).
Name and value limits (request format, response format):
- Names: alphanumeric plus
-and_ - Values: allowed character set documented on those pages; maximum about 4 KB per header value
Invalid names or values can trigger Cloudflare 1xxx errors such as Error 1040 (restricted header) or Error 1041 (invalid value).
Restrictions and common mistakes on header transform rules
Section titled “Restrictions and common mistakes on header transform rules”Request rules cannot change every header. Cloudflare documents these constraints explicitly:
You cannot modify or remove HTTP request headers whose name starts with x-cf- or cf- except for the cf-connecting-ip HTTP request header, which you can remove.
— Cloudflare, Request Header Transform Rules
Additional request-side pitfalls:
- Forbidden header names (e.g.
Accept-Encoding) generally cannot be modified. - Visitor IP headers (
x-forwarded-for,true-client-ip,x-real-ip,x-forwarded-proto) cannot be set to custom values in transform rules; Snippets or Workers onfetch()may work for some cases, with valid IP syntax and cross-zone limits described in the same page. - Removing
x-forwarded-forin a rule does not keep it off the origin: the proxy re-adds it before the origin unless Workers handled the request earlier. - Cookie request headers cannot be set or modified, only removed (all cookie headers on matching requests).
- Expression result
""or undefined removes the header instead of setting an empty value. - Filter expressions in a phase use original field values, not headers already changed by an earlier rule in that phase (Field values during rule evaluation).
Response-side restrictions (Response Header Transform Rules):
- No add/modify/remove for names starting with
cf-orx-cf-. - Cannot change certain headers such as
server,eh-cache-tag, oreh-cdn-cache-control. - Rules still apply to default and custom error pages.
- IP lists are not available in response rule expressions today.
Common mistakes
- Using Set on
Set-Cookiewhen you need multiple cookies — use Add (response remarks). - Expecting request transforms in the browser Network panel — check origin logs or Cloudflare Trace instead; Logpush logs original headers, not transformed ones.
- Enabling Managed Transforms on production without staging — docs warn they can break sites; disable and use partial custom rules if needed.
Managed Transforms vs custom Transform Rules
Section titled “Managed Transforms vs custom Transform Rules”Managed Transforms apply pre-built header changes zone-wide (bot protection request headers, visitor IP header add/remove, security response headers, remove X-Powered-By). Enabling one deploys internal transform rules that do not appear in the dashboard rule list and do not count toward plan rule limits.
Examples from Available Managed Transforms:
- Add security headers — adds
x-content-type-options,x-xss-protection,x-frame-options,referrer-policy,expect-ct(may break resources or certs; test first). - Remove visitor IP headers — strips
cf-connecting-ip,x-forwarded-for,true-client-ipon the request to origin (mutually exclusive with Add True-Client-IP on Enterprise).
For bespoke logic, custom Request/Response Header Transform Rules or Snippets remain the flexible path.
Gateway HTTP policies: another way to change request headers
Section titled “Gateway HTTP policies: another way to change request headers”On Cloudflare One, Allow HTTP policies can modify request headers before traffic reaches a destination, with TLS decryption required (Modify HTTP request headers in Gateway).
| Operation (UI) | API field | Behaviour |
|---|---|---|
| Delete | delete_headers |
Remove header |
| Overwrite | set_headers |
Replace value; create if missing |
| Add | add_headers |
Append; keep existing values |
Gateway applies operations in order: delete, then overwrite, then add. Up to 20 header operations per policy; names up to 256 bytes, values up to 4 KB. Dynamic values use @{identity.email}, @{source.ip}, @{device.posture}, and similar (changelog); unresolved variables become strings like cf-unresolved in the header and warnings in HTTP logs.
Use Gateway when headers must reflect authenticated Zero Trust identity on client egress traffic. Use zone Request Header Transform Rules when headers should be added on proxied public zone traffic to your origin (Request Header Transform Rules vs HTTP policies).
How to check this on your site
Section titled “How to check this on your site”- Deploy the rule on a narrow expression first (for example a single path prefix as in the bot score examples).
- For response headers, reload the page and inspect Response Headers in browser DevTools or curl the URL; confirm added CORS or
X-Bot-Scorevalues match the rule. - For request headers, inspect your origin access logs or a debug endpoint that echoes request headers; do not rely on the browser alone (troubleshooting).
- In Cloudflare, use trace or logging tools described in Troubleshoot Transform Rules to confirm the rule matches expected traffic; remember Logpush shows pre-transform headers unless you log at the origin.
- You can check automatically in Answerwell (our tool): https://answerwell.org/workspace
Report an error or ask a question
Section titled “Report an error or ask a question”If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact wording you are asking about.