Web and technology

_headers file in Cloudflare Pages: cache, security and noindex

By Edgar Jaimes 4 min read

Close-up of programming code on a screen

Learn how to use the Cloudflare Pages _headers file to control cache, security and noindex on your website, with its real limits and the mistakes to avoid.

You publish your new website on a Monday. On Tuesday you search for your business name on Google and, among the results, a strange address ending in pages.dev shows up with the same content as your domain. Nobody created it on purpose, but there it is, competing with you.

There is a simple way to prevent it, and to improve speed and security along the way: a text file of a few lines called _headers.

What is the _headers file in Cloudflare Pages?

It is a plain text file, with no extension, where you define HTTP headers that Cloudflare Pages adds to your site's responses. According to the Cloudflare documentation, it must live in your project's static files directory (in frameworks like Astro, the public/ folder) and Cloudflare processes it without publishing it as just another file.

Headers are invisible instructions that travel with every page: they tell the browser how much to keep in cache, tell Google whether to index something, or what permissions the page has. If you already migrated your site as we explained in the guide to migrating from WordPress to Cloudflare Pages, this is the next fine-tuning step.

Server cabinets with colorful network cables
Headers are applied when each response is delivered, before it reaches the visitor. Photo: Unsplash

How do you write a rule?

The first line is the path (or a pattern) and the indented lines below it are name: value pairs. An example based on the documentation:

/secure/page
  X-Frame-Options: DENY
  X-Content-Type-Options: nosniff

/static/*
  Access-Control-Allow-Origin: *

These are the pieces you need to know:

ElementWhat it doesDetail from the documentation
* (splat)Matches all charactersOnly one per URL; reused as :splat
:name (placeholder)Captures part of the pathEach name can be referenced only once
! NameRemoves a headerWrite the name with ! in front
Several rules matchThe headers are added togetherIf one is repeated, the values are joined with a comma

Which headers are worth setting up?

Three uses cover almost every case for a business website: cache, security and indexing control.

For cache, the documentation shows a rule for files with a fingerprint in the name (those that change name when their content changes): Cache-Control: public, max-age=31556952, immutable on /static/*. That way the browser does not request those files again. This adds to what we explained in the guide to website speed and Core Web Vitals.

For security, Cloudflare gives example rules with X-Frame-Options, X-Content-Type-Options, Referrer-Policy, Permissions-Policy and Content-Security-Policy. Start with the simplest ones and test your site after each change: a content policy that is too strict can break scripts or fonts you do need.

Heads up: copying a security policy from another site without testing it is the fastest way to break your own page. Add one header at a time and check that everything loads.

How do I keep my pages.dev address from showing up on Google?

Add a rule that sets X-Robots-Tag: noindex on your project's pages.dev address. It is an example from Cloudflare's own documentation: the path is https://:project.pages.dev/*, with that header below it.

Google explains in its documentation on robots meta and X-Robots-Tag that any rule valid in a robots meta tag also works in this header, and that it is the recommended option for non-HTML files, such as PDFs or images.

Common mistake: blocking in robots.txt the URL you want to deindex. Google only sees the noindex if it can crawl the page; if robots.txt blocks it, the rule is ignored. If you use robots.txt for AI bots, also check our guide to robots.txt and AI bots.

What limits and priority rules does it have?

The maximum is 100 rules, with a cap of 2,000 characters per line. In addition, three behavior rules prevent surprises:

  • Redirects from _redirects are applied before headers, so a redirect wins over a _headers rule. In the redirects documentation the default code is 302 and 301, 302, 303, 307 and 308 are accepted.
  • The headers in _headers replace the default values Cloudflare sends.
  • They are not applied to responses generated by Pages Functions. If your project uses them, define the headers inside the Function's code.
Lines of HTML code from a website template on a dark screen
A short file, but with an effect on every page of the site. Photo: Unsplash

How do I check that it works?

Publish the change and check the actual headers of the response, not just the file. In the browser, open the developer tools, Network tab, select the page or file and look at the response headers. If one is missing, check the indentation, that the path matches and that no redirect or Function is interfering.

Then, ask Google to recrawl what you changed from Search Console, as explained in the Search Console and sitemap guide. And if you still do not know what the Cloudflare plan you use includes, here is the free plan guide.

In short

That pages.dev address that showed up next to your domain is solved with three lines in a text file. _headers is one of the cheapest improvements you can make to a static website: better cache, basic security and control over what Google indexes. If you want us to set it up and test it on your site, learn about our hosting and domains service.

Frequently asked questions

Where does the _headers file go in an Astro project?

In the public/ folder, next to your other static files. When the site is built it is copied to the output, and Cloudflare Pages reads it without serving it as a file.

How many rules does _headers support?

According to Cloudflare's documentation, a maximum of 100 rules, and each line can have up to 2,000 characters counting spaces, header name and value.

Can I stop my pages.dev address from showing up on Google?

Yes. Cloudflare's own documentation proposes a rule for https://:project.pages.dev/* with the X-Robots-Tag: noindex header.

Does _headers work if I use Pages Functions?

Not for the responses a Function generates. In that case, headers are defined inside the Function's code.

What happens if a URL has both a _headers rule and a redirect?

The redirect is applied first, so it wins over the headers rule.

Do not stop at the theory: apply it to your business

We implement hosting and domains end to end, with your real operation. The first conversation is free.

Keep reading