How to Add Security Headers and Rewrite URLs with Cloudflare Transform Rules

Adding X-Frame-Options, Referrer-Policy, and Permissions-Policy headers—or stripping noisy X-Powered-By headers—usually means editing Nginx blocks, .htaccess files, or application middleware across every origin server you manage. If you host static sites alongside WordPress or Drupal origins behind Cloudflare, Transform Rules let you standardize HTTP headers and rewrite request paths at Cloudflare’s edge in one place before responses reach visitors.

Editorial illustration of Cloudflare Transform Rules for URL rewrites and HTTP response security headers

Editorial illustration: Transform Rules let you adjust URIs, request headers, and response headers at the edge on proxied DNS records.

Summary

  • Per Cloudflare’s Transform Rules documentation, Transform Rules cover three distinct actions on proxied traffic: URL Rewrite Rules (path/query rewrites), Request Header Transform Rules, and Response Header Transform Rules (plus one-click Managed Transforms).
  • URL Rewrite Rules alter the URI before it reaches your origin without changing the URL in the visitor’s browser bar—unlike Redirect Rules (301/302), which send a redirect response back to the browser.
  • Response Header Transform Rules let you add security headers (X-Content-Type-Options, X-Frame-Options, Referrer-Policy) and remove headers (X-Powered-By, Server version hints) across static sites and CMS origins alike.
  • Test header changes with curl -I and check browser DevTools—especially when adding Content-Security-Policy or X-Frame-Options, which can block legitimate embeds or payment iframes if configured too strictly.

Three Transform Rule types (and when to use each)

  1. URL Rewrite Rules: Change the path or query string transparently at the edge (for example, stripping tracking query parameters like fbclid before cache lookup, or routing /legacy-docs internally to /docs).
  2. HTTP Request Header Modification: Set or remove headers sent from Cloudflare to your origin (such as adding a shared internal routing header or stripping untrusted client headers before your app reads them).
  3. HTTP Response Header Modification: Set or remove headers sent from Cloudflare to the visitor’s browser—ideal for consistent security headers across static pages, WordPress, and Drupal.

Cloudflare also includes Managed Transforms (one-click toggles for common adjustments like adding X-Bot-Score or removing X-Powered-By—check your plan’s available options in the dashboard).

Step-by-step: Add clean security headers at the edge

1. Confirm your DNS record is proxied (orange-clouded)

As Cloudflare’s Transform Rules docs explicitly note, Transform Rules only execute on DNS records proxied through Cloudflare. DNS-only (gray-clouded) records bypass Cloudflare’s edge completely.

2. Create a Response Header Transform Rule

In the Cloudflare dashboard, go to Rules > Transform Rules > Modify Response Header (or start from a template under Rules > Overview):

  • Rule name: Add Baseline Security Headers
  • When incoming requests match: All incoming requests (or scope to http.host eq "example.com")
  • Set static:
    • X-Content-Type-Options: nosniff
    • X-Frame-Options: SAMEORIGIN
    • Referrer-Policy: strict-origin-when-cross-origin
    • Permissions-Policy: geolocation=(), microphone=(), camera=()
  • Remove:
    • X-Powered-By

Using Set overwrites any duplicate header sent by your origin, whereas Add appends a second header—use Set for security headers like X-Frame-Options so browsers do not receive conflicting values.

3. Keep HSTS and caching in their dedicated sections

Do not hand-code Strict-Transport-Security inside a Transform Rule if you already manage it under SSL/TLS > Edge Certificates > HSTS—keeping HSTS in one place avoids conflicting max-age values (see how to enable Cloudflare HSTS safely). Similarly, manage edge cache lifetimes with Cache Rules and CDN-Cache-Control headers rather than trying to rewrite Cache-Control blindly for every path.

4. Verify live headers with curl -I

After saving the rule, test both an HTML page and an admin/API endpoint:

curl -I https://example.com/

Confirm your new headers appear once (not duplicated), verify that X-Powered-By is gone, and check that embedded YouTube videos, payment popups, or WordPress block previews still load properly under X-Frame-Options: SAMEORIGIN.

For complementary Cloudflare protection on staging and login URLs, combine Transform Rules with Cloudflare Access Zero Trust and tiered WAF rate limiting.

Common mistakes

  • Confusing URL Rewrite Rules with Redirect Rules. A URL Rewrite changes what your origin sees without updating the browser address bar; if you moved a blog post permanently for SEO, use a 301 Redirect Rule or _redirects instead (see choosing between _redirects and Bulk Redirects).
  • Setting X-Frame-Options: DENY when your CMS or third-party tool embeds pages in an iframe. SAMEORIGIN allows your own domain’s iframes (such as WordPress previews or internal embeds) while still blocking external clickjacking frames.
  • Deploying a strict Content-Security-Policy immediately without Report-Only testing. A typo in script-src can break Google Analytics, Turnstile widgets, or CMS block editors—test CSP in Content-Security-Policy-Report-Only first before enforcing it.

Why this matters

Origin servers and third-party plugins often ship inconsistent or missing HTTP security headers. Standardizing baseline response headers inside Cloudflare Transform Rules protects every static asset and dynamic CMS route consistently without duplicating config across Apache, Nginx, and Workers.

My Take

Start with a single Response Header Transform Rule setting X-Content-Type-Options, X-Frame-Options: SAMEORIGIN, Referrer-Policy, and Permissions-Policy while removing X-Powered-By. It takes two minutes, works on Cloudflare’s Free plan, and immediately cleans up security-header audit warnings across your whole site.

Conclusion

Use Cloudflare Response Header Transform Rules with Set (not Add) for baseline browser security headers, keep HSTS and caching in their dedicated dashboard sections, use URL Rewrite Rules only for internal path normalization (not SEO 301 redirects), and verify every change with curl -I.

Sources