Blog · 7 min read

HTTP security headers explained: how to add them

Published

HTTP security headers explained: how to add them

Every time someone opens your website, your server sends back a few lines of instructions before the page itself. Some of those lines, called security headers, tell the visitor's browser how to protect them. They cost nothing, they work on any website, and a surprising number of sites send none of them.

This guide applies to any website, whether it runs on WordPress, Shopify, Wix, Joomla, Magento or custom code. Where you add the headers differs by host, but the headers themselves are the same.

What security headers do

Browsers are cautious by default, but they cannot know which behaviour is safe for your particular site. Headers let you say so. A few lines can stop attackers framing your pages, injecting scripts or downgrading visitors to an unencrypted connection. They are not a replacement for updates and strong passwords, but they block whole classes of attack at almost no cost.

The six headers worth setting

Strict-Transport-Security (HSTS)

Tells browsers to use https:// only for your site, for a set time. This stops attackers downgrading a visitor to plain http:// on public Wi-Fi. A common value is max-age=31536000; includeSubDomains. Make sure your SSL certificate renews reliably first, because with HSTS an expired certificate blocks visitors completely. See our guide on an expired SSL certificate.

Content-Security-Policy (CSP)

Lists which scripts, styles, images and frames your pages may load. If an attacker injects a script from another domain, the browser refuses to run it. It is the strongest header, and also the easiest to get wrong, so build it carefully with the Content-Security-Policy generator.

X-Content-Type-Options

The value nosniff stops browsers guessing file types. Without it, an uploaded file can be treated as a script.

X-Frame-Options

Use SAMEORIGIN so other sites cannot show your pages inside a hidden frame and trick visitors into clicking. The modern alternative is frame-ancestors inside your CSP.

Referrer-Policy

Controls how much of your page address is shared with sites your visitors click through to. strict-origin-when-cross-origin is a sensible default that keeps private URLs private.

Permissions-Policy

Switches off browser features you do not use. For example camera=(), microphone=(), geolocation=() stops any script on your page from asking for them.

Step 1: check which headers you send today

Run your address through the security headers checker. It grades your site from A to F, lists what is missing and gives copy-paste fixes. To see the raw headers yourself, use the HTTP header checker.

Step 2: add the headers

  1. Nginx: add lines such as add_header X-Content-Type-Options "nosniff" always; to your server block, then reload Nginx.
  2. Apache or shared hosting: add Header always set X-Content-Type-Options "nosniff" to your .htaccess file. The mod_headers module must be enabled.
  3. CDN or proxy: most CDNs have a rules or transform section where response headers can be added without touching the server.
  4. Website builders and hosted shops: you may not control headers directly. Check the platform's settings or ask support which ones are already set.

Step 3: test again, and roll out the CSP carefully

Reload your site in a private window and run the checker again. For the Content-Security-Policy, start in report-only mode. The browser then reports what it would have blocked without actually blocking anything, so you can fix gaps before switching to enforcement.

Common mistakes

Common questions

Will security headers improve my search rankings?

Not directly. They protect visitors and keep your site trustworthy, which supports everything else you do, but they are not a ranking factor on their own.

Can a security header break my site?

Yes, mainly the Content-Security-Policy and HSTS. Test on a staging copy or in report-only mode first, and keep a note of what you changed so you can undo it.

Do I need all six?

Aim for all six. The first four are quick wins on almost any site, and the Content-Security-Policy is worth the extra effort once the others are in place.

Free tools for this problem

More guides