Skip to main content

How to Add Security Headers to a Static Website

Published: •Last updated: Guides
security headerscontent security policystatic website hostingnetlifycloudflare pages

A static website sets its response headers with a _headers file at the root of what you publish: a line with a path, then the headers for that path on the lines under it, indented. Netlify, Cloudflare Pages and Statpio all read this format, so one file can send a Content-Security-Policy, X-Frame-Options, Referrer-Policy and Permissions-Policy with every page. On Statpio it works on every plan, Free Forever included, and Statpio sets the HTTPS header (Strict-Transport-Security) and nosniff on every website itself.

Disclosure: this guide is published by Statpio, one of the tools compared. We link every competitor fact to its official source.

Why does a static site need a _headers file?

Headers are part of the server's answer, not of the page, and on a static host you don't run the server. A <meta> tag can't stand in for some of the headers scans ask for: MDN notes that X-Frame-Options in a meta element "has no effect", and that a CSP's frame-ancestors isn't supported there either. So when a client's security scan lists missing headers, the fix is a file your host reads.

How does a _headers file work?

Save a plain text file named _headers, with no extension, next to your index.html, so it sits at the top of the upload. With a framework, put it in the folder that is copied into the build as it is, usually public/, as Cloudflare's docs also suggest. Then write:

  • A path line, starting with / at the start of the line: an exact address (/contact), a * on its own at the end for everything in a folder (/fonts/*, or /* for the whole site), or :name for one part of the address (/team/:person).
  • Header lines under it, indented: Name: value. Lines that start with # are comments.

Every rule that matches an address applies, in the order of the file, and a header given by two rules is sent once with both values joined by ", ". A line ! Name takes off a header an earlier rule of the file gave, so a narrower rule can set it again with another value. Statpio reads up to 100 paths and 500 header lines, in a file of up to 64 KB.

Which headers should I add? Four examples to copy

All four can go in the same file.

A site-wide security block

/*
  Content-Security-Policy: default-src 'self'; script-src 'self' 'unsafe-inline' https://challenges.cloudflare.com; style-src 'self' 'unsafe-inline'; img-src 'self' data:; frame-src 'self' https://challenges.cloudflare.com https://onstatpio.com; frame-ancestors 'self'; base-uri 'self'; object-src 'none'
  X-Frame-Options: SAMEORIGIN
  Referrer-Policy: strict-origin-when-cross-origin
  Permissions-Policy: camera=(), microphone=(), geolocation=()
  • Content-Security-Policy lists where a page may load scripts, styles, images and frames from. This one allows what Statpio adds to pages (see below); add the address of anything else your pages load, such as web fonts, analytics, a video player or your app's API.
  • X-Frame-Options and the CSP's frame-ancestors stop other sites from showing your pages in a frame. Choose SAMEORIGIN and 'self' rather than DENY and 'none' if you use Point at the page on Studio Pro or Agency Pro: it shows your website in a frame on the website's own address.
  • Referrer-Policy decides how much of your address other sites see when a visitor follows a link. strict-origin-when-cross-origin is the browsers' default, and Statpio already sends it; set another policy, such as no-referrer, to replace it.
  • Permissions-Policy turns off browser features your site doesn't use: () means the feature is off for the page and any frame in it.

CORS for web fonts

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

Browsers load web fonts from @font-face with CORS (MDN's CORS guide lists them), so fonts hosted on one site and used on another need this header. * lets any site use them; to allow just one, name it instead: Access-Control-Allow-Origin: https://www.example.com.

Long caching for fingerprinted files

/assets/*
  Cache-Control: public, max-age=31536000, immutable

Use it only on files whose names change when their content does, such as a build's assets folder with a hash in every file name. Browsers then keep them for a year without checking again, the pattern MDN calls cache busting. If another rule also gives these files a Cache-Control, the two values are joined into one contradictory header, which Cloudflare's docs warn about too. In the later, narrower rule, write ! Cache-Control and then the new value.

Keep a folder out of search results

/drafts/*
  X-Robots-Tag: noindex

Google reads any rule from a robots meta tag in this header too, and it works for PDFs and images, which can't carry a meta tag (Google's robots meta tag docs). Don't also block the folder in robots.txt: Google ignores the indexing rules of a page it isn't allowed to crawl. And noindex keeps pages out of search results, not out of reach; for private pages, use a password.

What does Statpio set itself, and what does it skip?

Statpio sends Strict-Transport-Security and X-Content-Type-Options: nosniff with every page and file of every website, and Referrer-Policy: strict-origin-when-cross-origin unless your file sets another. Your headers are added on top, and your Cache-Control replaces Statpio's, with these exceptions:

  • Lines that are skipped: Set-Cookie; Content-Type (set from the file name); Strict-Transport-Security and X-Content-Type-Options (already set); Location (redirects go in a _redirects file); connection headers such as Content-Length, Content-Encoding and Transfer-Encoding; Vary, ETag, Date, Last-Modified and CDN-Cache-Control; anything starting with X-Statpio- or Cf-; and Netlify's Basic-Auth. A path line that starts with http (a rule for one domain), or has a * anywhere but on its own at the end, is skipped with its headers.
  • Caching that stays Statpio's: a password-protected website keeps Statpio's private caching whatever the file says, and missing pages (404) get your headers but keep Statpio's caching.
  • Answers the file doesn't reach: redirects, and the pages Statpio serves itself, such as the password page and the hosted blog.

The file is part of each version you publish: change it, publish again, and the new headers apply.

What must a Content-Security-Policy allow on Statpio?

Statpio adds a few things to the pages it serves: small inline scripts for your forms and the visitor count, inline styles for its form widgets, and the Turnstile spam check from challenges.cloudflare.com. A policy that blocks them stops your forms and statistics quietly, while the page itself still loads. So:

  • script-src needs 'unsafe-inline' and https://challenges.cloudflare.com;
  • frame-src needs https://challenges.cloudflare.com and, on your own domain, https://onstatpio.com, which shows the check there;
  • style-src, if your policy sets it or default-src, needs 'unsafe-inline';
  • script-src must not hold a nonce or a hash: MDN notes that browsers then ignore 'unsafe-inline'.

Cloudflare's Turnstile CSP notes list the same two Turnstile entries, and the Website check (check_website) that AI assistants run through Statpio's connector after publishing lists the lines it skipped and warns when a policy would block Statpio's scripts. The trade-off is real: MDN says 'unsafe-inline' "defeats much of the purpose of having a CSP". If a scan insists on a policy without it, that policy will block Statpio's forms and visitor counts.

Does the same file work on Netlify and Cloudflare Pages?

For the most part. Netlify says to save _headers "to the publish directory of your site" (Netlify custom headers), and Cloudflare reads it from "the static asset directory of your project" (Cloudflare Pages headers); both use a path line with indented headers, as of November 2026. When you bring a file to Statpio, check three things:

  • netlify.toml: Netlify also takes headers in its configuration file. Statpio reads only _headers, so move any [[headers]] blocks into it.
  • Wildcards: both allow a * inside a path, as in /*.jpg. Statpio skips that rule; use a folder instead, such as /images/*.
  • Detach lines: ! Name comes from Cloudflare's version of the format, where it can also remove a default header. On Statpio it removes only what an earlier rule of your file set, never Statpio's own headers.

Moving a whole site from Netlify? Import a website in the Statpio dashboard brings its files, _redirects and _headers over, with its form messages if you tick them. Our Statpio vs Netlify comparison covers the rest, and Cloudflare Pages alternatives compares other static hosts.

Ready to add your headers?

Write the file, put it next to index.html and publish: the publish your website guide shows the upload, and the features page lists what every plan includes. To try it on a free onstatpio.com address, start free on Statpio.

Frequently asked questions

How do I check that my security headers are applied?

Open the page with your browser's developer tools: on the Network tab, select the page and read its response headers. Or run curl -I with the page's address, which fetches the headers only. If one is missing, check that the file is named _headers, with no extension, at the top of the upload; that each path line starts with / and isn't indented; and that the header isn't one Statpio skips. An AI assistant connected to Statpio can run the Website check, which lists the lines it skipped and why.

Can I set a longer Strict-Transport-Security or HSTS preload on Statpio?

No. Statpio sends Strict-Transport-Security with every website itself, and a line for it in your _headers file is skipped, preload included. On an onstatpio.com address it asks browsers to remember HTTPS for a year; on your own domain, for a day, so a domain that is later pointed elsewhere isn't held to HTTPS for long. A scan that wants a longer time on your own domain will still report it.

Why did my fonts, images or videos stop loading after I added a Content-Security-Policy?

Because the policy doesn't name where they come from. Each outside address has to be in the matching part of the policy: font-src for web fonts, style-src for a font service's stylesheet, img-src for images, frame-src for an embedded video player and connect-src for an API your scripts call. The browser's console names every address it blocked: add it, publish again and reload.

Should I send X-Frame-Options if my CSP has frame-ancestors?

Sending both is common. frame-ancestors is the newer control, and MDN points to it for more options than X-Frame-Options gives, while X-Frame-Options also covers older browsers. Keep the two saying the same thing: SAMEORIGIN with 'self', or DENY with 'none'.

Can a _headers file password-protect a folder on Statpio?

No: Netlify's Basic-Auth lines are skipped. Use the website's own password instead (Settings, then Password Protection), on every plan: visitors see a password page before anything else, and protected pages are never kept in public caches. It covers the whole website, so put private pages on a website of their own.

Keep reading