Opens in a new tab

Strict-Transport-Security (HSTS): make browsers use HTTPS only

Strict-Transport-Security tells browsers to reach your site over HTTPS only, for as long as max-age says: every http:// link is upgraded before it leaves the browser, and a certificate error can no longer be clicked through. It is one line of configuration, and the one to roll out carefully, because browsers remember it.

Defined inRFC 6797 (2012)
Send itOver HTTPS only — browsers ignore it over HTTP
Recommendedmax-age=31536000; includeSubDomains
Remove it withmax-age=0, sent over HTTPS
Browser supportEvery current browser

Check HSTS on your site

Enter a domain: we fetch its homepage and read every security header, this one first.

What it does

Once a browser has seen the header on an HTTPS response, it treats the host as HTTPS-only until max-age runs out: it rewrites http:// to https:// before connecting, and on a certificate error it refuses the connection outright, with no option to proceed. That closes the gap an attacker on the network uses to downgrade a visitor to plain HTTP or to present a forged certificate.

The header only counts when it arrives over HTTPS; browsers ignore it over HTTP, and the HTTP-to-HTTPS redirect should not carry it. Each response that carries it resets the clock to max-age from now. The weak spot is the very first visit, before the browser has seen the header — the gap the preload list was made to close.

Directives and values

Directive or valueWhat it does
max-age=<seconds>Required. How long the browser remembers to use HTTPS only. 31536000 is one year; 0 tells the browser to forget the rule.
includeSubDomainsThe rule covers every subdomain too — only safe once every subdomain, internal ones included, serves HTTPS.
preloadNot part of the standard: consent to be added to the browsers’ built-in list. On its own it does nothing — you still submit the domain. See HSTS preloading.

What to send

Strict-Transport-Security: max-age=31536000; includeSubDomains

Start small and raise it: hstspreload.org suggests max-age=300 (five minutes), then 604800 (a week), then 2592000 (a month), waiting out each stage before the next, and only then a year or two. A long max-age set by mistake keeps visitors locked to HTTPS — and out of the site if its certificate lapses — until it runs out.

How to set it

Apache

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

In the virtual host or .htaccess; needs mod_headers (and AllowOverride FileInfo for .htaccess). always also covers error pages and redirects, which plain Header set skips.

nginx

add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

In the server block. A location with any add_header of its own drops every add_header from the server block — repeat them there, or from nginx 1.29.3 use add_header_inherit merge;.

IIS (web.config)

<system.webServer>
  <httpProtocol>
    <customHeaders>
      <add name="Strict-Transport-Security" value="max-age=31536000; includeSubDomains" />
    </customHeaders>
  </httpProtocol>
</system.webServer>

Caddy

header Strict-Transport-Security "max-age=31536000; includeSubDomains"

Cloudflare

SSL/TLS → Edge Certificates → HTTP Strict Transport Security (HSTS) → Enable HSTS
Max Age: 12 months · Include subdomains: on · Preload: off

On every plan. Cloudflare warns: turn on HTTPS first, and once HSTS is on, do not pause Cloudflare, switch the record to DNS-only or let the certificate lapse — visitors would be locked out for the whole max-age.

WordPress

add_action( 'send_headers', function () {
	header( 'Strict-Transport-Security: max-age=31536000; includeSubDomains' );
} );

In a small plugin or the theme’s functions.php. PHP only runs for pages WordPress builds, so images, scripts and other static files do not get the header — set it at the server or CDN to cover everything.

Common mistakes

  • Sending it over HTTP or on the redirect to HTTPS, where browsers ignore it.
  • Turning on includeSubDomains before every subdomain serves HTTPS — including internal ones and plain-HTTP services such as certificate revocation servers.
  • Starting with a max-age of a year or more: if the certificate later expires, visitors cannot reach the site until max-age runs out.
  • Setting it only on example.com while visitors go straight to www.example.com, which never sees it — each host should send its own.
  • Sending the header twice: browsers use only the first.

What we recommend

Send max-age=31536000; includeSubDomains once every subdomain serves HTTPS, reached through the short-max-age stages above. Add preload only if you have read what preloading commits you to — most sites no longer need it.

Questions

How do I turn HSTS off?

Send Strict-Transport-Security: max-age=0 over HTTPS. Browsers forget the rule the next time they load the site over HTTPS; there is no way to switch it off over plain HTTP, by design.

Does HSTS replace the redirect to HTTPS?

No. Keep the permanent redirect from http:// to https:// for the first visit and for browsers that have not seen the header; HSTS makes every later visit skip plain HTTP entirely.

Sources

Checked on 25 September 2026 against these official sources:

Know when a header disappears

Add your domain and we scan it on a schedule — security headers, certificate, HTTPS, DNS and email — and email you when a security header is removed, a certificate changes or HTTPS stops redirecting. Free for your first domain.

Create a free account