Security Headers for WordPress: Copy-Paste Setup for Apache and LiteSpeed

Add HSTS, nosniff, Referrer-Policy, Permissions-Policy and frame protection to WordPress with one .htaccess block, plus how to start with CSP safely.

Security Headers for WordPress – Techono guide cover

Quick answer: Security headers are short instructions your server sends with every page. They tell browsers to always use HTTPS, not to guess file types, and not to let other sites frame your pages. On Apache or LiteSpeed you can add the main ones with one block in .htaccess. Add HSTS only after HTTPS works everywhere.

Copy-paste block for .htaccess

Put this at the top of .htaccess in your site’s root folder. Keep a backup of the old file.

<IfModule mod_headers.c>
  Header always set Strict-Transport-Security "max-age=31536000"
  Header always set X-Content-Type-Options "nosniff"
  Header always set Referrer-Policy "strict-origin-when-cross-origin"
  Header always set Permissions-Policy "camera=(), microphone=(), geolocation=()"
  Header always set X-Frame-Options "SAMEORIGIN"
  Header always set Content-Security-Policy "frame-ancestors 'self'; upgrade-insecure-requests"
</IfModule>

What each header does

  • Strict-Transport-Security (HSTS): browsers use HTTPS for your site for one year, even if someone types http://.
  • X-Content-Type-Options: nosniff: browsers do not guess file types. This blocks some upload tricks.
  • Referrer-Policy: other sites see only your domain, not the full page address, when visitors click away.
  • Permissions-Policy: turns off camera, microphone and location access your site does not use.
  • X-Frame-Options and frame-ancestors: stop other sites from showing your pages inside a frame (used in click-jacking).
  • upgrade-insecure-requests: asks browsers to load old http:// files over HTTPS.

Be careful with HSTS

  • Add it only when every page, and every subdomain you use, works over HTTPS.
  • Browsers remember it for the full max-age. If HTTPS breaks, visitors cannot get in until you fix it.
  • Add includeSubDomains and preload only if you plan to join the browser preload list. The list at hstspreload.org requires a max-age of at least one year, includeSubDomains and preload.

Content-Security-Policy: start in report-only mode

A full CSP limits which scripts may run. It is the strongest protection against cross-site scripting, but on WordPress it easily breaks plugins, ads and analytics. Test first with the report-only version. It changes nothing for visitors and shows problems in the browser console:

Header always set Content-Security-Policy-Report-Only "default-src 'self'; script-src 'self' 'unsafe-inline'; img-src 'self' data: https:; style-src 'self' 'unsafe-inline'"

Open your main pages, press F12 and read the warnings. Add the domains you really use, then switch to the normal header.

Headers you do not need

  • X-XSS-Protection: old and no longer used by modern browsers. MDN advises against it.
  • Expect-CT: deprecated, browsers no longer need it.

Not on Apache or LiteSpeed?

On Nginx, use add_header Name "value" always; in the server block. Many hosts and CDNs, like Cloudflare, can also add headers for you.

Check it with our free tools: HTTP Headers Checker · SSL Certificate Checker

Sources

Jonathan

More from the SimpliCloud Blog

Security Headers for WordPress: Copy-Paste Setup for Apache and LiteSpeed

Security Headers for WordPress: Copy-Paste Setup for Apache and LiteSpeed

Add HSTS, nosniff, Referrer-Policy, Permissions-Policy and frame protection to WordPress with one .htaccess block, plus how to start with CSP safely.

Why Your Site Says “Not Secure” and How to Fix It

Why Your Site Says “Not Secure” and How to Fix It

The four reasons browsers show "Not Secure", how to fix each one on WordPress and cPanel, and why SSL certificates now expire faster.

Need a Tool We Don’t Have?

Tell us what you check on your sites every week. The most-requested tools get built first.