Add HTTP security headers to WordPress via .htaccess

Add HTTP security headers to WordPress via .htaccess

HTTP security headers tell the browser how to treat your site’s content, and they block whole classes of attacks: clickjacking, MIME sniffing, some kinds of content injection. WordPress does not send them by default. On Apache they are added with one block in .htaccess, without any PHP, which makes them one of the quickest security improvements.

Problem: The site sends no security headers, so the browser cannot protect visitors from clickjacking, MIME sniffing and injected content.

Solution: Set the headers with mod_headers in .htaccess, above the WordPress block.

The headers

<IfModule mod_headers.c>
    # Do not allow other sites to put our pages into an iframe (clickjacking)
    Header always set X-Frame-Options "SAMEORIGIN"

    # Do not let the browser guess the content type
    Header always set X-Content-Type-Options "nosniff"

    # Send only the origin, not the full URL, to other sites
    Header always set Referrer-Policy "strict-origin-when-cross-origin"

    # Browser features the pages do not use
    Header always set Permissions-Policy "camera=(), microphone=(), geolocation=()"

    # HTTPS only, for a year. Enable only when HTTPS works on all subdomains!
    # Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
</IfModule>

What they do:

  • X-Frame-Options: SAMEORIGIN allows iframes only on your own domain. Use DENY if the site never frames itself.
  • X-Content-Type-Options: nosniff makes the browser trust the Content-Type of the server, so an uploaded file cannot be run as a script.
  • Referrer-Policy controls how much of the address is sent to other sites in the Referer header.
  • Permissions-Policy turns off browser APIs (camera, microphone, geolocation) for the pages and any embedded frames.
  • Strict-Transport-Security (HSTS) makes the browser use HTTPS only, for the time in max-age. It is commented out on purpose: once a browser has received it, the site cannot go back to HTTP until the time runs out. First set up the redirect to HTTPS and make sure the certificate renews correctly.

Content-Security-Policy

CSP is the most powerful header: it lists the sources from which the page may load scripts, styles, images and fonts. It is also the easiest one to break a site with, because WordPress, themes and plugins use inline scripts and external services (fonts, analytics, video). Start in report-only mode, which only logs violations in the browser console:

Header always set Content-Security-Policy-Report-Only "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com; img-src 'self' data: https:;"

Open the main pages and the admin, add the sources the console complains about, and only then switch the header name to Content-Security-Policy.

Check

curl -I https://example.com/

The response must contain the headers. A service like securityheaders.com shows them with a grade and explanations.

NOTE: If the site is behind Cloudflare or a similar proxy, the headers can also be set there; do not set them in both places with different values. Headers protect the browser side; file access rules are a separate layer, see protecting wp-config.php and xmlrpc.php.