Article
HSTS explained in plain terms (and how to roll it out without locking yourself out)
Published 2026-11-09
HSTS explained in plain terms (and how to roll it out without locking yourself out)
HSTS is one response header. Your server tells the browser "this site is HTTPS only, remember that for N seconds", and the browser obeys. There is nothing to install and nothing to pay for. This single header does something your HTTP redirect cannot: it closes the window where a first plain-HTTP request can be hijacked before it ever reaches your server.
The catch is in the second half of my title. Roll HSTS out carelessly, with the wrong flags, and you can make parts of your own site unreachable for months, for visitors you cannot contact and cannot help. So this guide is in two halves: what the header does, and the order I would turn the knobs in.
Why your HTTP-to-HTTPS redirect is not enough
Almost every site with a certificate already redirects http://example.com to https://example.com. That redirect is the problem, not the solution. Here is the sequence that makes security people twitchy:
- A visitor types
example.cominto the address bar. Browsers tryhttp://first for a bare hostname, because nobody types a scheme. - That first request goes out in plaintext.
- Anyone positioned on the network path (public Wi-Fi, a compromised router, a corporate proxy, a nation-state box that snoops traffic) can answer it before your server does.
- The attacker serves a fake plain-HTTP version of your site and simply keeps the visitor there. Your real redirect never runs, because the attacker answered first. Your certificate never enters the picture, because the victim never asks for HTTPS.
This attack is called SSL stripping, and it has been demonstrated publicly since 2009. It requires no broken cryptography and no bug in your stack. It exploits the fact that the very first hop of the very first visit has no protection, because the browser does not yet know the site has HTTPS at all.
A redirect answers an HTTP request with "go away, use HTTPS instead". An attacker who answers first with "here is the site, no HTTPS needed" wins that race. HSTS removes the race: after the browser has seen the header once, it upgrades every future request to HTTPS internally, before any packet leaves the machine. There is no plaintext request to intercept, so there is nothing to strip.
What the header actually says
Strict-Transport-Security: max-age=31536000; includeSubDomains
Read it as: remember this host is HTTPS-only for 31,536,000 seconds (one year), and the rule covers every subdomain too. The mechanism is documented in RFC 6797, published by the IETF in 2012, and browsers have supported it for well over a decade.
Three things happen once a browser stores your site in its HSTS list:
- Every future
http://example.comURL is rewritten tohttps://example.combefore connecting. Port 80 becomes 443. This happens on the browser side, so no network round-trip is wasted and no plaintext request exists. - If the certificate for your site is invalid or expired, the browser refuses to let the user click through the warning. This is deliberate. The classic phishing flow of "here is a scary warning, click Advanced, click Proceed" does not work on an HSTS host, because an on-path attacker is exactly who that click-through serves.
- The entry is keyed to your hostname and applies to every port, 443 included.
Two details from the RFC that trip people up. The browser only records the header when it arrives over a valid HTTPS connection. Send Strict-Transport-Security over plain HTTP and the browser ignores it, because otherwise a man-in-the-middle could inject or strip HSTS policy on hosts that never opted in. And the entry is per-hostname: example.com and www.example.com are separate HSTS hosts, which is why subdomain coverage has its own directive.
One more quirk worth knowing: each response carrying the header refreshes the clock. A returning visitor with max-age=31536000 who visits once a week never lapses. HSTS only expires if the browser stops hearing from you entirely for the whole max-age window.
The three directives, and the respect each deserves
max-age is the only required one. It is measured in seconds, which makes the values look alien: 300 is five minutes, 86400 is one day, 31536000 is a year, 63072000 is two years.
includeSubDomains extends the policy from example.com to every hostname under it: blog.example.com, mail.example.com, old-dev-box.example.com. This is where I see the most self-inflicted damage. The directive does not check whether those subdomains have certificates. It tells the browser they are all HTTPS-only regardless. If a subdomain that still runs plain HTTP gets traffic from a browser that has stored your policy, that subdomain becomes unreachable in that browser. The browser upgraded the request to HTTPS and nothing is listening there, so the user sees a connection error rather than a warning they could ignore.
Before you add includeSubDomains, inventory your DNS. Every record pointing at a web service needs a certificate covering that hostname. The one that gets forgotten is rarely the production site; it is the old staging box, the router admin panel, the internal tool on a hostname nobody documented.
preload does not change browser behavior at all. It is a statement of intent: "my domain qualifies for the browser vendors' built-in HSTS lists, and I consent to being included". The lists are maintained at hstspreload.org and shipped inside Chrome, Firefox, Safari and Edge, so a fresh browser that has never visited your site already knows it is HTTPS-only. That closes the remaining first-visit gap from the SSL-stripping section, the one plain HSTS cannot fix.
Qualifying requires all three directives at once: max-age of at least a year, includeSubDomains, and the preload token. Then a human reviews the submission. And here is the part I want you to sit with before sending it: preload removal is slow and not guaranteed. The removal pages at hstspreload.org are candid that getting a domain out of the shipped lists takes months, and browsers on old versions, or Safari with its snapshot schedule, can carry your entry long after you regret it. Domains have been removed, but nobody at the browser vendor is in a hurry when the alternative is users getting stripped.
My honest position: preload is a commitment you earn, not a checkbox for launch day. A site with one hostname and no subdomains can preload early with little risk. A company with a messy internal DNS tree should treat it like a small migration project.
How to set it
In nginx, inside the server block that serves HTTPS:
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
The always parameter matters more than it looks. Without it, nginx only sets the header on successful responses. Your 404s and 500s would go out without HSTS, and any page that 404s loses the protection and stops refreshing the browser's clock.
In Apache, inside the VirtualHost or .htaccess:
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
Same story with always: the plain Header set form skips error responses.
If your site sits behind Cloudflare or a similar CDN, set it at the edge. Cloudflare has an HSTS toggle under SSL/TLS, Edge Certificates, with its own max-age picker and the two flags as checkboxes. Setting the header both at the edge and at your origin produces two conflicting values, and the browser takes the one it saw last, which is a fun thing to debug six months later. Pick one layer, preferably the edge, since it also covers cached responses.
For a Laravel application, resist the urge to put this in a middleware. The header belongs to the TLS layer, not the application. Application-sent HSTS misses the static assets your web server serves directly and vanishes the moment a route bypasses your middleware stack. nginx or the edge, not PHP.
The rollout order I would actually use
- Fix your redirect first. Every HTTP request should 301 or 308 to HTTPS, on the full hostname. If this is broken, HSTS is not your next step.
- Verify every subdomain you intend to cover has a working certificate.
includeSubDomainsis a promise about hostnames you may have forgotten, so go look. - Ship
max-age=300with no other directives. Five minutes. Nothing catastrophic can happen in five minutes, and you confirm the header actually reaches browsers. - Bump to
max-age=86400, still alone. A full day catches visitors who came in the morning and return in the evening, and it survives long enough to notice anything broken. OWASP suggests exactly this staging step. - Now add
includeSubDomains, keeping the small max-age for another few days. - Raise max-age to
31536000or63072000. I would stay at one year for the first cycle. - Only after a month of quiet: submit to hstspreload.org, if your subdomain situation genuinely supports it.
You can shorten this, and plenty of people do. The order exists because the failure modes are asymmetric. If you set too small a max-age, nothing happens and you fix it in a minute. If you set includeSubDomains; preload on day one over a subdomain that cannot do HTTPS, the affected visitors have the problem, not you, and it outlives your revert.
Checking your own site
From a terminal:
curl -sI https://example.com | grep -i strict-transport
Empty output means no HSTS header. If you get a line back, read the directives against what you intended.
To see what a browser has actually stored, Chrome has a viewer at chrome://net-internals/#hsts where you can query a domain and delete the stored entry. Deleting it there is the only way I know to un-break a testing browser after an over-eager includeSubDomains.
For the overall picture including HSTS, run a header check on your site. Our own report shows which security headers are present, which are missing, and explains each finding. It is the difference between "I set a header once" and "the header is on every response the site produces, including errors".
Common questions
Does HSTS affect SEO or rankings? It is not a ranking factor on its own. HTTPS is, and HSTS makes HTTPS stickier for returning visitors, which is where its value is. There is no downside for a site that is already fully HTTPS.
Can I undo HSTS if something breaks? Reduce max-age to 0 and serve that. Browsers that see it clear the stored entry. But browsers that do not revisit during your old max-age keep the old policy until it expires, and preload entries do not come out through the header at all. Undo is possible, it is just slower than the do.
Do I need HSTS if I have no login form and nothing to steal? The certificate exists to guarantee the visitor is talking to you and nobody rewrote the page on the way. If that guarantee matters, and it is most of the reason anyone buys a certificate, HSTS makes it apply to every visit instead of only the ones that happened to start with https://.
Want to see whether your site sends HSTS today, along with every other security header? CheckWebSecurity assesses your website's external perimeter — headers, TLS configuration and exposure — and reports findings with plain-language explanations. Start a free check.
Related: Content Security Policy explained in plain terms · How website security checks work · WordPress security scanning without plugins