What Is HSTS (HTTP Strict Transport Security)?
HSTS (HTTP Strict Transport Security) is a response header that tells a browser to only ever connect to a site over HTTPS for a set period of time, refusing plain HTTP entirely rather than just redirecting it. Once a browser has seen the header, it enforces HTTPS on its own, even if a bookmark, typed address or old link points to the http:// version. This closes the short window a downgrade attack could otherwise use on the very first request.
What the header actually does
When a browser loads a page over HTTPS and receives a Strict-Transport-Security header, it remembers that domain and the time limit given. For as long as that period lasts, the browser rewrites any attempt to reach the site over plain HTTP into HTTPS internally, before a network request is even sent. This differs from an ordinary 301 redirect to HTTPS, which still requires one initial unencrypted request that an attacker on the same network could intercept or tamper with before the redirect happens.
What the header looks like
# Apache (.htaccess or virtual host config)
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
# Nginx (server block)
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
- max-age: how many seconds the browser should enforce HTTPS for, in this example about one year.
- includeSubDomains: applies the rule to every subdomain as well, not just the exact domain that sent the header.
- preload: an optional flag requesting inclusion in browsers' built-in preload lists.
The HSTS preload list
Major browsers ship with a hardcoded list of domains that must always use HTTPS, even before the browser has ever visited them once. A site can request to be added through hstspreload.org. This protects the very first visit, which a header alone cannot do since the browser needs at least one successful HTTPS visit to learn the rule. Removal from the preload list takes time to propagate through browser updates, so it should only be requested once HTTPS is stable across the whole domain and its subdomains.
HSTS vs. a plain HTTPS redirect
| 301 redirect to HTTPS | HSTS header | |
|---|---|---|
| First request | Sent over HTTP, then redirected | Still sent over HTTP on the very first visit, unless preloaded |
| Later requests | Still start as HTTP, then redirect each time | Rewritten to HTTPS before leaving the browser |
| Vulnerable to interception | Yes, on every request's initial hop | Only on the very first request, unless preloaded |
| Setup | A redirect rule | A response header, plus a reliable certificate |
How to enable HSTS safely
- Confirm HTTPS works correctly on the main domain and every subdomain before adding
includeSubDomains, since one broken subdomain becomes unreachable over HTTP as well. - Start with a short
max-age, such as a few minutes or hours, and raise it gradually once you are confident HTTPS is stable. - Make sure certificate renewal is automated, since a lapsed certificate on an HSTS-protected domain locks visitors out rather than just showing a warning they could click past.
- Only request preload submission after the header has run reliably for a while at a long max-age.
Frequently asked questions
What does HSTS stand for?
HSTS stands for HTTP Strict Transport Security. It is a response header that forces browsers to use HTTPS only for a site, for a length of time the site itself specifies.
Is HSTS the same as an HTTPS redirect?
No. A redirect still sends one initial unencrypted request before switching to HTTPS, while HSTS has the browser rewrite the address to HTTPS internally, without any unencrypted request at all once the rule is remembered.
What happens if my SSL certificate expires while HSTS is active?
Visitors will be blocked from reaching the site at all, with no option to bypass the warning, since the browser refuses to fall back to HTTP. This makes automated certificate renewal especially important once HSTS is enabled.
How do I add the HSTS header to my site?
Add a Strict-Transport-Security header through your web server configuration, such as an Apache virtual host, an .htaccess file, or an Nginx server block, specifying a max-age value in seconds.
What is the HSTS preload list?
It is a list built into major browsers of domains that must always use HTTPS, even on the very first visit before the browser has seen the site's own header. Sites apply for inclusion at hstspreload.org.
Can I undo HSTS once I turn it on?
You can set max-age to 0 to clear it for browsers that revisit the site, but any browser that cached the earlier, longer max-age will keep enforcing HTTPS until that time expires. Preloaded domains take even longer to remove since the list ships inside browser updates.
Related topics
- What Is an SSL Certificate? Security & SSL
- 301 vs. 302 Redirect Errors & Troubleshooting
- What Is a Web Server? Web Servers
- Virtual Hosts Explained Web Servers
- What Is a .htaccess File? Web Servers