How to add an HSTS header

Send a Strict-Transport-Security header so browsers use HTTPS for every visit, starting with a one-year max-age and nothing else.

Last reviewed

Why it matters

Without HSTS, a visitor who types the bare domain or follows an http link makes a first request over plain HTTP, which an attacker on the same Wi-Fi can intercept before the redirect to HTTPS happens. HSTS tells the browser to use only HTTPS for the site for max-age seconds.

How to fix it

Start with max-age=31536000 (one year) and nothing else. includeSubDomains is a later step, once every subdomain (mail, staging, old client portals) serves valid HTTPS; preload comes after that and takes months to undo. Browsers ignore the header over plain HTTP, so sending it on HTTPS is enough.

Replace the highlighted values: www.example.com with the site's hostname.

Cloudflare

Use Cloudflare's HSTS setting rather than a header rule:

  1. SSL/TLS > Edge Certificates > HTTP Strict Transport Security (HSTS), select Enable HSTS, read the dialog, select I understand, then Next.
  2. Turn Enable HSTS (Strict-Transport-Security) on and set Max Age Header (max-age) to 12 months (six months or more avoids Owl Keeper's "HSTS lasts only" finding).
  3. Leave Apply HSTS policy to subdomains (includeSubDomains) and Preload off for now, and Save.

It applies only to proxied hostnames. While it is on, Cloudflare warns against switching records to DNS only, pausing Cloudflare, moving nameservers away, redirecting HTTPS to HTTP, or letting the certificate break: each locks returning visitors out. If the origin sends HSTS too, keep one. To turn it off later, set Max Age Header to 0 (Disable).

Sources: Cloudflare HSTS, proxy status.

nginx

  1. In the server block that listens on 443, add:
add_header Strict-Transport-Security "max-age=31536000" always;
  1. Run sudo nginx -t, then reload.

Without always the header is left off error responses. A location block with any add_header of its own (often Cache-Control for static files) drops the inherited ones, so repeat the line there, include a shared snippet in each, or on nginx 1.29.3 and later add add_header_inherit merge;.

Source: ngx_http_headers_module.

Apache

Add to the site's .htaccess (it needs AllowOverride FileInfo):

<IfModule mod_headers.c>
    Header always set Strict-Transport-Security "max-age=31536000"
</IfModule>

The IfModule wrapper avoids a 500 error if mod_headers is not loaded, but then fails silently, so check with curl. If curl shows the header twice, use the pattern from Apache's documentation:

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

Sources: mod_headers, AllowOverride.

Caddy

In the site block:

www.example.com {
	header Strict-Transport-Security "max-age=31536000"
}

Behind reverse_proxy, if the app may send its own, write header >Strict-Transport-Security "max-age=31536000": the > applies it after the proxied response. Reload Caddy.

Source: the header directive.

Vercel

Vercel already sends Strict-Transport-Security: max-age=63072000 on custom domains. If the finding appears on a Vercel site, something in front of Vercel (another CDN or proxy) is answering for the hostname. Do not add a one-year value here: it would shorten Vercel's default.

Source: Vercel encryption.

Netlify

Check with curl first: Netlify does not document whether it sends HSTS itself. Then add a _headers file in the publish directory:

/*
  Strict-Transport-Security: max-age=31536000

or the same in netlify.toml:

[[headers]]
  for = "/*"
  [headers.values]
    Strict-Transport-Security = "max-age=31536000"

These headers are not applied to Functions, Edge Functions, SSR pages or proxied content, which must set it themselves.

Source: Netlify custom headers.

WordPress

Plugin route: install HTTP Headers (by Dimitar Ivanov), go to Settings > HTTP Headers > Security, select Edit next to Strict-Transport-Security, select On, set max-age to 1 year, leave includeSubDomains and preload unticked, and Save Changes. Its default mode writes .htaccess, which does nothing on an nginx host: there, copy the lines from its Manual setup tab into the server config.

Code route: create wp-content/mu-plugins/security-headers.php (must-use plugins load automatically) and keep every header from these guides in this one function:

<?php
/*
 * Plugin Name: Security headers
 */
add_action( 'send_headers', function () {
    header( 'Strict-Transport-Security: max-age=31536000' );
} );

send_headers runs for WordPress pages only: not for static files the server sends directly, and not for pages a full-page cache serves. Where you control nginx or Apache, the server is the better place.

Sources: HTTP Headers plugin, send_headers, must-use plugins.

Shopify

Merchants cannot set it: Shopify keeps HSTS active on every connected domain with a 90-day max-age. A Shopify store should not show this finding, though it will show the shorter-than-six-months one, which the merchant cannot change. Ask Shopify Support about HSTS.

Source: Shopify domains terminology.

Check that it worked

curl -sI https://www.example.com/ | grep -i strict-transport-security

Exactly one line should come back (if the hostname redirects, test the final one or add -L). Check an error page too, by requesting an address that does not exist. In the browser, devtools > Network, reload, and look at the document's response headers. Then run the Owl Keeper Security headers check again.

Test it now with the free HTTP status code checker. No account needed.

What can go wrong

  • Once a browser has seen it, plain HTTP is refused for max-age seconds, and visitors cannot click through certificate warnings. If the certificate lapses or HTTPS is switched off, returning visitors are locked out until it is fixed.
  • includeSubDomains locks out any subdomain without valid HTTPS.
  • preload is close to permanent: removal takes months to reach browsers.
  • On Cloudflare, switching to DNS only or pausing Cloudflare while HSTS is on breaks access.