In this guide
The headers and what they do
| Header | What it does | Priority |
|---|---|---|
| Strict-Transport-Security (HSTS) | Tells browsers to only use HTTPS for your site | High, once HTTPS works everywhere |
| X-Content-Type-Options: nosniff | Stops browsers from guessing file types, which can turn an upload into a script | High, safe to add |
| X-Frame-Options / frame-ancestors | Stops other sites from embedding yours in a frame (clickjacking) | Medium, safe for most sites |
| Referrer-Policy | Limits how much of your URLs is shared with other sites | Medium, safe to add |
| Permissions-Policy | Disables browser features you don’t use (camera, microphone…) | Low to medium |
| Content-Security-Policy (CSP) | Controls which scripts and resources may load. Strong protection against injected scripts | High value, but needs careful setup |
Safe headers to add (Apache)
Add to your root .htaccess (requires the mod_headers module, which most hosts have):
<IfModule mod_headers.c>
Header always set X-Content-Type-Options "nosniff"
Header always set X-Frame-Options "SAMEORIGIN"
Header always set Referrer-Policy "strict-origin-when-cross-origin"
Header always set Permissions-Policy "camera=(), microphone=(), geolocation=()"
</IfModule>
HSTS: add only when your whole site, including subdomains you include, works over HTTPS:
Header always set Strict-Transport-Security "max-age=31536000"
Start with a shorter max-age if you’re unsure. Browsers remember HSTS, so a mistake is hard to undo quickly.
On Nginx, the same headers go into the server configuration with add_header. Ask your host if you can’t edit it.
Content-Security-Policy: the powerful one
A good CSP is one of the best defenses against injected JavaScript, the kind used in redirects, card skimmers and fake CAPTCHAs. But WordPress sites load scripts from many places. The safe way:
- Start with
Content-Security-Policy-Report-Onlyso nothing is blocked yet. - Watch the browser console or a reporting service to see what would be blocked.
- Build the allowed list, then switch to the real
Content-Security-Policyheader.
Check your result
After adding headers, test your site with a free header-checking tool and click through it: forms, checkout, embeds and the admin area.
From my cleanup work: on online stores, a well-built Content-Security-Policy is one of the few things that can stop a card skimmer even after it’s been injected, because the browser refuses to send data to the attacker’s domain. It takes some setup, but for shops it’s worth it.