HSTS, or HTTP Strict Transport Security, is a web security policy defined in RFC 6797 that tells browsers to connect to a site only over HTTPS for a set period. The server sends a Strict-Transport-Security header over HTTPS, and the browser then upgrades any HTTP request for that host to HTTPS internally before sending it. This stops downgrade and cookie hijacking attacks, but it only protects repeat visitors unless the domain is also preloaded.
Key takeaways
- HSTS is a Strict-Transport-Security header sent over HTTPS that makes browsers use HTTPS only for that host for the max-age period.
- It closes the gap a plain HTTP-to-HTTPS redirect leaves open: the first unencrypted request that an attacker can intercept or downgrade.
- Preload adds first-visit protection, but it is hard to undo — test with a short max-age first and enable it only when HTTPS works on every subdomain.
Your site redirects HTTP to HTTPS, so you are safe — right? Not quite. That first HTTP request still travels unencrypted, and an attacker on the network can intercept it before the redirect ever happens. HSTS exists to close exactly that gap.
What is HSTS?
HSTS, or HTTP Strict Transport Security, is a policy mechanism defined in RFC 6797 that tells browsers to interact with a website only over HTTPS. The server declares the policy with a Strict-Transport-Security response header, sent over an HTTPS connection.
Once a browser has received that header, it remembers the policy for the period in the header — the max-age — and upgrades any later HTTP request for that host to HTTPS internally, before anything leaves the device. No HTTP request is ever sent, so there is nothing for an attacker to intercept or downgrade, as RFC 6797 specifies.
HSTS protects against two attacks in particular: protocol downgrade, where an attacker strips HTTPS and forces plain HTTP, and cookie hijacking, where a session cookie sent over HTTP is stolen and reused. It is a browser-layer policy, not a server feature, which is why it only works after the browser has learned it.
How does HSTS work?
When you first visit a site over HTTPS, the server includes the Strict-Transport-Security header in its response. Your browser stores that policy, tied to the host name, for the number of seconds in max-age.
On your next visit, if you type http://example.com or click an old HTTP link, the browser does not send an HTTP request at all. It rewrites the address to HTTPS internally — often shown in developer tools as a 307 internal redirect — and connects securely. The server never sees the HTTP version.
There are two limits to this. First, the header is only honoured when it arrives over HTTPS; a header sent over plain HTTP is ignored, so an attacker cannot plant or shorten a policy that way. Second, the first visit is unprotected, because the browser has not learned the policy yet. That is the trust-on-first-use gap, and preload is the answer to it.
What does the HSTS header look like?
The header is a single line with up to three directives. The most common production value is Strict-Transport-Security: max-age=63072000; includeSubDomains; preload, which sets a two-year policy for the host and all its subdomains and signals eligibility for the preload list.
Directives are not case-sensitive and can appear in any order, but each should appear only once. The header must be sent on HTTPS responses; sending it over HTTP has no effect, and a meta http-equiv tag for HSTS is ignored under RFC 6797, as the HSTS specification summary confirms.
Check your own site's header with our free security headers checker, which reports the Strict-Transport-Security header and its directives alongside the rest of your security headers.
What do max-age, includeSubDomains and preload mean?
max-age is the only required directive. It is the number of seconds the browser should remember the policy, counted from when the header is received. A value of 31536000 is one year; 63072000 is two years.
| Directive | What it does | Risk if misused |
|---|---|---|
| max-age | Required. How long, in seconds, the browser enforces HTTPS-only for the host | Too long locks in a mistake; too short gives weak protection |
| includeSubDomains | Extends the policy to every subdomain of the host | Breaks any subdomain that does not support HTTPS |
| preload | Signals the site meets preload requirements; not part of RFC 6797 | Hard to undo; removal takes months to reach browsers |
includeSubDomains is powerful because it protects cookies scoped to the parent domain, but it is also the directive most likely to break something: every subdomain the browser might hit must speak proper HTTPS, or users get hard failures. Audit every subdomain — including staging, internal tools and legacy services — before adding it.
preload is not part of RFC 6797 at all. Browsers introduced it independently, and the token in a header is only a request for inclusion, not confirmation of it. To be accepted, a site must serve a valid certificate, redirect HTTP to HTTPS on the same host, serve HTTPS on every subdomain, and send a base-domain header with a max-age of at least one year plus includeSubDomains and preload.
What is HSTS preload and should you use it?
HSTS preload is a list of domains built into browsers, maintained through hstspreload.org. If your domain is on the list, the browser enforces HTTPS from the very first connection, without waiting to learn the policy — which closes the trust-on-first-use gap.
That sounds like a clear win, and for high-value sites it is. But preload is close to irreversible: removal is a separate submission that takes months to reach users through browser updates, and hstspreload.org itself does not recommend preloading for most sites. Many browsers already upgrade HTTP navigations to HTTPS on their own, so preload mainly helps when those upgrades fail against an active attacker.
The safe path is to start with a short max-age, such as 300 seconds, monitor whether any subdomains or services break, then increase gradually to one or two years, and only then submit for preload. Do not ship preload by default in a configuration generator, and do not submit until you are certain every subdomain will stay on HTTPS permanently.
How do I enable HSTS?
Enable HSTS in stages, and test at each stage. The rollout below is the one recommended by hstspreload.org and the OWASP cheat sheet, and it avoids the most common way HSTS breaks a site: turning it on at full strength before HTTPS is solid everywhere.
- Fix HTTPS first. Serve a valid certificate, redirect all HTTP traffic to HTTPS on the same host, and make sure every subdomain works over HTTPS.
- Start with a short max-age, such as
max-age=300, without includeSubDomains or preload. Watch logs and user reports for a few days. - Increase max-age to a week, then a month, then a year, checking at each step that nothing breaks.
- Add includeSubDomains only after you have audited every subdomain and confirmed each one serves HTTPS.
- Consider preload last, and only if you need first-visit protection and can commit to HTTPS permanently.
Where you set the header depends on your stack: in your web server configuration for Apache or Nginx, in a Cloudflare transform rule, or in your application framework. Set it on the base domain over HTTPS, and make sure it persists through any redirects from that domain, because the preload check requires it on the redirect response too.
What are the common HSTS mistakes?
The most damaging mistake is enabling HSTS with includeSubDomains before every subdomain supports HTTPS. Users then hit hard failures on the subdomain that does not, with no click-through to get past the error, and the fix only takes effect after the policy expires or is corrected.
- Enabling HSTS before HTTPS is stable. A broken certificate under HSTS means no access at all, not a warning page.
- Adding preload too early. Inclusion is hard to unwind, and removal takes months to reach users.
- Sending the header over HTTP. Browsers ignore it there, so it does nothing — and it may give a false sense of security.
- Forgetting redirects. If HTTPS itself redirects, that redirect response must still carry the HSTS header, or preload checks fail.
- Assuming HSTS replaces the redirect. Search engines still need the server-side 301 to understand the move and carry link equity.
HSTS is deliberately unforgiving: a browser that knows you as an HSTS host will hard-fail with no click-through if your certificate ever breaks. That is the point — it stops attackers from clicking through too — but it means you should only enable it once HTTPS is genuinely solid across every subdomain. Check your certificate as well as your headers with our free SSL checker.
HSTS vs HTTPS redirect: what is the difference?
An HTTP-to-HTTPS redirect is a server instruction: when a browser asks for the HTTP version, the server replies "go to the HTTPS version instead". HSTS is a browser instruction: the browser remembers the site is HTTPS-only and never asks for the HTTP version in the first place.
You need both. The redirect handles first-time visitors, search engines and anyone whose browser has not learned the policy. HSTS handles repeat visitors and stops an attacker from intercepting the request before the redirect fires. Neither replaces the other, and a site with only one of them is weaker than a site with both.
The same logic applies to the rest of your site's transport security. HSTS does not fix a weak certificate, an outdated TLS version or a missing redirect — it assumes those are already right and then enforces HTTPS on top of them.
The bottom line
HSTS is a small header with a large effect: it tells browsers to use HTTPS only, closes the first-request gap a redirect leaves open, and stops downgrade and cookie hijacking attacks for repeat visitors. Preload extends that to first-time visitors, at the cost of being hard to undo.
Roll it out in stages — short max-age first, then a year or two, then subdomains, then preload — and verify each step. You can check whether your site already sends the header with our free security headers checker.
Frequently asked questions
What is HSTS in simple terms?
HSTS is a promise a website makes to your browser: always use HTTPS for this site. Once your browser has seen the promise, it upgrades any HTTP address for that site to HTTPS before sending anything, so an attacker on the network cannot downgrade the connection.
Is HSTS the same as HTTPS?
No. HTTPS is the encrypted protocol itself. HSTS is a policy on top of HTTPS that tells the browser to refuse to use plain HTTP for that site. You need a working HTTPS setup first; HSTS then makes sure browsers actually use it.
What is the HSTS preload list?
The preload list is a list of domains built into browsers, maintained through hstspreload.org. Browsers enforce HTTPS for listed domains from the very first visit, closing the gap that HSTS alone leaves open. Inclusion requires a valid certificate, HTTPS on all subdomains, and a header with a max-age of at least one year plus includeSubDomains and preload.
How long should max-age be?
Start short, such as 300 seconds or one day, while you test. Once HTTPS is stable, use one year (31536000 seconds) or two years (63072000 seconds). Longer is stronger, because it protects visitors for longer, but it also means a mistake takes longer to expire.
Can HSTS break my website?
Yes, if HTTPS is not working properly. A browser that knows a site as an HSTS host will refuse to connect if the certificate is invalid, with no click-through option. HSTS with includeSubDomains can also break subdomains that do not support HTTPS, so check every subdomain first.
Do I still need an HTTP to HTTPS redirect if I use HSTS?
Yes. HSTS is a browser policy, not a server redirect. Search engines and first-time visitors still need the server-side 301 redirect to understand the move. HSTS sits on top of the redirect; it does not replace it.
How do I check if a site has HSTS enabled?
Use your browser's developer tools: open the Network tab, load the site over HTTPS, click the main request and look for the Strict-Transport-Security response header. You can also use a free security headers checker, which reports the header and its directives.
Can I remove a site from the HSTS preload list?
Yes, but slowly. Removal is a separate submission through hstspreload.org, and it takes months to reach users because browsers only update their built-in list with new releases. That is why preload is described as close to irreversible, and why you should not enable it by default.
Sources
The engineers and analysts who build Cysoro's domain scanner, email-authentication checks and free security tools. We test what we write about every day — SPF, DKIM, DMARC, certificates, look-alike domains and phishing — and check each guide against the relevant standards, government guidance and vendor documentation.
Checked against RFC 6797, MDN's Strict-Transport-Security reference, the OWASP HSTS cheat sheet and hstspreload.org.
How we write and fact-check →