Free rules / Cloudflare _headers

Cloudflare Pages _headers Rules for AI Coding (free)

Ask an AI tool to “add security headers” to a Cloudflare Pages site and it often writes a Vercel or Netlify config, defines the same header twice, or assumes the file also covers server-rendered pages. This free rules file tells your agent how Cloudflare's _headers file really works and makes it check the live site before calling it done.

Works in: a Cursor rule (.cursor/rules/), CLAUDE.md, AGENTS.md or Windsurf rules · Checked against: Cloudflare's Pages headers and Workers static-assets headers docs on

What AI tools get wrong, and the fix

The agent does thisWhat Cloudflare actually does
Writes headers in vercel.json, netlify.toml or next.configOn Cloudflare Pages, static-asset headers go in a plain-text _headers file in the deployed output (usually via public/ or static/)
Sets the same header in /* and in a narrower ruleCloudflare applies every matching rule and joins repeated values with a comma, so the browser gets a merged value you didn't mean
Puts ! Header-Name above the broad ruleRules run in file order, so a detach only removes what earlier rules already added; put the narrow rule after the broad one
Says SSR pages and API routes are covered_headers is not applied to responses from Pages Functions, _worker.js or Worker code; set those headers on the Response in code
Builds a giant multi-line CSPMax 100 header rules and 2,000 characters per line; keep each header on one line under the limit
Declares it fixed without checkingcurl -sI the live site, check a static file and a Functions route separately

How to use it

  1. Cursor: save the file below as .cursor/rules/cloudflare-headers.mdc. It attaches when you edit _headers, Wrangler config or Functions code.
  2. Claude Code, Codex and others: paste everything below the --- header into CLAUDE.md or AGENTS.md.
  3. Then ask: “List which routes are static and which come from Functions, then propose a _headers file and the curl checks before editing.”

The rules file

---
description: Cloudflare Pages / Workers static assets: security headers with the _headers file (placement, rule order, detach, limits, Functions/SSR caveat, testing)
globs: **/_headers, **/wrangler.toml, **/wrangler.json, **/wrangler.jsonc, functions/**, **/_worker.js
alwaysApply: false
---
# Cloudflare _headers rules

This project deploys to Cloudflare Pages (or Workers with static assets). Checked against Cloudflare's Pages and Workers static-assets headers docs on 2026-11-16.
If you are unsure whether a behavior still holds, read https://developers.cloudflare.com/pages/configuration/headers/ instead of guessing.

## Where the file goes
- The file is named exactly `_headers` (no extension). It must end up in the deployed static asset / build output directory.
- With a framework, put it in `public/` (or `static/`) so the build copies it into the output. With no framework, put it straight in the folder that gets deployed.
- Cloudflare parses it and does not serve it. Don't link to it or reference it from code.
- Don't put security headers in `netlify.toml`, `vercel.json` or `next.config` for this project; they do nothing on Cloudflare.

## Syntax
- Each block: first line is a URL or pattern (`/*` = everything), then indented `Name: value` lines. Lines starting with `#` are comments.
- Only one `*` splat per pattern. Named placeholders like `:project` are allowed; each placeholder can be used once.
- Absolute URLs must start with `https` and can't include a port.
- Limits: at most 100 header rules, and each line at most 2,000 characters (spacing, name and value all count). Keep the CSP on one line and under that limit.

## Rule order and repeated headers (the common bug)
- Every rule whose pattern matches applies. Rules run in file order.
- If two matching rules set the same header, Cloudflare joins the values with a comma. Never define the same security header (CSP, X-Frame-Options, Cache-Control, HSTS) in two rules that can match the same URL.
- To give a narrower path a different value: put the narrow rule AFTER the broad one, detach with `! Header-Name`, then set the new value. A detach placed above the broad rule does nothing.
- When a URL matches both `_redirects` and `_headers`, the redirect wins.

## Functions, SSR and Worker code
- `_headers` does NOT apply to responses generated by Pages Functions (`functions/` or an advanced-mode `_worker.js`) or by Worker code (including `assets.run_worker_first`), even if the URL matches.
- If the app renders on the server (SSR adapters, API routes in Functions), set security headers on the `Response` in that code too, including CORS on `OPTIONS` responses.
- Before claiming headers are "done", list which routes are static and which come from Functions/Worker code.

## Default security block (adjust, don't paste blindly)
- Baseline for `/*`: `X-Content-Type-Options: nosniff`, `X-Frame-Options: DENY`, `Referrer-Policy: strict-origin-when-cross-origin`, a `Permissions-Policy` that turns off features the app doesn't use, and a `Content-Security-Policy` with `frame-ancestors 'none'`.
- Add every outside origin the app really loads (analytics, Stripe, Supabase, Firebase, fonts) to the matching CSP directive, or those requests get blocked. Ask the user which services they use instead of guessing.
- Ship a new CSP as `Content-Security-Policy-Report-Only` first, click through the app with devtools open, fix what it reports, then switch to the enforcing header.
- HSTS: add `includeSubDomains; preload` only if every subdomain serves HTTPS. If HSTS is already on in the Cloudflare dashboard, set it in one place only.
- To keep the `*.pages.dev` copy out of search results once a custom domain exists: `https://:project.pages.dev/*` with `X-Robots-Tag: noindex`.

## Verify, don't assume
- After deploy, check real responses: `curl -sI https://<site> | grep -iE "content-security|x-frame|x-content-type|referrer|permissions|strict-transport"`.
- Check one static file and one Functions/SSR route separately. Report any header that shows up twice or comma-joined.
- Never say headers are fixed based on the file alone.

Example _headers file

A starting point, not a final policy. The CSP ships as Report-Only first so nothing breaks; add the outside origins your app loads, then switch to Content-Security-Policy.

# public/_headers  (copied into your build output; Cloudflare reads it and doesn't serve it)
/*
  X-Content-Type-Options: nosniff
  X-Frame-Options: DENY
  Referrer-Policy: strict-origin-when-cross-origin
  Permissions-Policy: camera=(), microphone=(), geolocation=()
  Content-Security-Policy-Report-Only: default-src 'self'; img-src 'self' data:; frame-ancestors 'none'; base-uri 'self'; form-action 'self'

# a page that must be embeddable on your own site: detach, then set again (must come AFTER /*)
/embed/*
  ! X-Frame-Options
  X-Frame-Options: SAMEORIGIN

# keep the pages.dev copy out of search once you have a custom domain
https://:project.pages.dev/*
  X-Robots-Tag: noindex

Common questions

Where does the _headers file go on Cloudflare Pages?

In the static asset directory that gets deployed. With a framework, put it in public/ or static/ so the build copies it into the output folder; with no framework, put it straight in the folder you deploy. Cloudflare parses it and does not serve it as a file.

Why is my header value repeated with a comma?

When more than one rule matches a URL and both set the same header, Cloudflare joins the values with a comma. Cloudflare's docs show this producing conflicting Cache-Control directives. Define each header once per path, or detach it with ! Header-Name in a later rule and set it again.

Do _headers rules apply to Pages Functions or SSR pages?

No. Cloudflare's docs say custom headers in _headers are not applied to responses generated by Pages Functions (a functions/ folder or advanced-mode _worker.js), even when the URL matches. Attach those headers to the Response in your Functions code.

Does the same file work on Cloudflare Workers?

Yes for static assets. Workers with static assets read the same _headers file with the same rules and limits, and the same exception: responses generated by your Worker code (including with assets.run_worker_first) don't get them.

Sources: Cloudflare's Pages headers and Workers static-assets headers docs, read on Nov 16, 2026. On Vercel with Next.js instead? See the Next.js 16 security headers block. For more launch checks, see the free ship-safe rules.

Step-by-step Cloudflare Pages headers guide and free header scan on ShipCheck (sister project)

NEWSLETTER

Get new cartridges and free rules in your inbox. About 2 emails a month, no spam, unsubscribe anytime.

Opens Buttondown in a new tab. Confirm via the email they send. Past issues