Contents
Even with an http-to-https redirect, that first http request travels in the clear. On public Wi-Fi an attacker can intercept it and suppress the redirect, keeping the visitor on an unencrypted version without them noticing. This is called SSL stripping, and HSTS is the cure.
How it works
-
A user visits your site over HTTPS and receives the header:
Strict-Transport-Security: max-age=31536000; includeSubDomains -
The browser records that this domain is HTTPS-only for
31536000seconds, one year. -
On later visits the browser rewrites any
http://link tohttps://internally, before sending anything. -
If there’s a certificate error, the browser doesn’t offer a “proceed anyway” button.
The directives
| Directive | Meaning |
|---|---|
max-age |
How long to remember the policy, in seconds |
includeSubDomains |
Apply the policy to every subdomain |
preload |
Ask for the domain to be built into browsers’ preload list |
Example: adding it with .htaccess
<IfModule mod_headers.c>
Header always set Strict-Transport-Security "max-age=300"
</IfModule>
Start short, at 300 (five minutes), check that the site and all subdomains work over HTTPS, then raise it to a day, then to a year.
Risks and common mistakes
includeSubDomainswith a subdomain that has no certificate, such as an internal panel on http: browsers can no longer reach it.- Rushing into
preload: removal from the list is slow, and the list requires at least a year’s max-age plusincludeSubDomains. - An expired certificate with HSTS: visitors can’t bypass the warning at all, so renewal must be automatic.
- Testing changes on your own machine: the browser remembers the policy even after you remove the header. In Chrome you can delete it for one domain at
chrome://net-internals/#hstswhile testing.
On ArabyHost
The free certificate is issued and renewed automatically through AutoSSL and Let’s Encrypt, which is the key requirement for using HSTS safely. Add the header in the .htaccess file in your site’s folder; the LiteSpeed web server supports these directives. First turn on the redirect as in the force HTTPS guide, then add HSTS once you’ve confirmed every subdomain has a valid certificate.
FAQ
Does HSTS replace the 301 redirect to HTTPS?
No. The browser only learns the HSTS policy after a first visit over HTTPS, so you still need the 301 redirect for that first visit; HSTS handles the ones after.
How do I undo HSTS if something breaks?
Send the header with max-age=0 and browsers drop the policy on their next HTTPS visit. If the domain is on the preload list, getting it removed takes a long time.
Does HSTS affect SEO?
It isn't a direct ranking factor, but it replaces a server redirect with an in-browser one, so pages open slightly faster for returning visitors.