Adding security headers with .htaccess

Add HTTP security headers — HSTS, X-Content-Type-Options, Referrer-Policy, frame protection and Permissions-Policy — safely with .htaccess.

Guide2 min readLast updated
On this page
  1. A safe starting set
  2. HSTS
  3. Content-Security-Policy
  4. Checking
  5. Common questions

Security headers tell browsers to apply extra protections to your site.

A safe starting set

<IfModule mod_headers.c>
 Header always set X-Content-Type-Options "nosniff"
 Header always set Referrer-Policy "strict-origin-when-cross-origin"
 Header always set X-Frame-Options "SAMEORIGIN"
 Header always set Permissions-Policy "camera=(), microphone=(), geolocation=()"
</IfModule>
HeaderProtects against
X-Content-Type-OptionsBrowsers guessing a file is executable
Referrer-PolicyLeaking full URLs to other sites
X-Frame-OptionsYour pages being framed by other sites (clickjacking)
Permissions-PolicyPages using browser features they don’t need

These rarely break anything. If your site embeds itself in frames on another domain, or uses the camera or location, adjust accordingly.

HSTS

Once HTTPS works for the domain and every subdomain you use:

Header always set Strict-Transport-Security "max-age=31536000"

Browsers then refuse plain HTTP for a year. Don’t add includeSubDomains or preload unless every subdomain has working HTTPS.

Content-Security-Policy

CSP is the most powerful header and the easiest to get wrong. Start in report-only mode:

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

Check the browser console for what would be blocked, adjust, then switch to Content-Security-Policy.

Checking

Open developer tools → Network, select the page request and view Response Headers, or use an online security headers checker.

Prefer to follow along?

New to Traxio? The PHP hosting page lists the PHP versions, database and tools every account gets.

Common questions

What are security headers?

They are instructions your site sends with each page telling the browser how to behave — for example, to refuse to load the page inside someone else’s frame. You add them in .htaccess, and a sensible basic set takes a few minutes.

This is a general web-hosting explanation, not specific to Traxio.

What is HSTS?

HSTS is a header telling browsers to only ever reach your site over HTTPS, so they refuse to try plain HTTP at all. Add it once HTTPS is working reliably — it is deliberately hard to undo, because browsers remember it.

This is a general web-hosting explanation, not specific to Traxio.

That memory is the thing to be careful about. If you enable HSTS with a long duration and something later breaks your certificate, visitors cannot click past the warning. Start with a short duration.

Spotted something out of date or unclear? Tell us and we’ll fix it.

Keep going