How to Enable HSTS Safely (Nginx, Apache)
To enable HSTS, send a Strict-Transport-Security response header over HTTPS and raise its max-age in stages. HTTP Strict Transport Security (HSTS) is a response header that tells a browser to use HTTPS for a set period, which removes the insecure first hop after the first visit. It is also hard to undo, so roll it out in stages.
The header
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
| Directive | Meaning | Risk |
|---|---|---|
| max-age | Seconds the browser remembers the rule | Long values lock you in |
| includeSubDomains | Applies to every subdomain | Breaks any subdomain without valid HTTPS |
| preload | Consent to be listed in browsers' built-in list | Removal can take months |
Before you enable HSTS
- The whole site works on HTTPS, with no mixed content. Scan with the broken link checker and the browser console.
- The certificate renews reliably. If it expires under HSTS, visitors cannot click past the warning.
- You know every subdomain. Check with the DNS lookup if you plan to use includeSubDomains.
- HTTP redirects to HTTPS with a 301. See the HTTPS redirect generator.
Enable HSTS in stages
| Stage | Header | Wait |
|---|---|---|
| 1 | max-age=300 (5 minutes) | The full 5 minutes, then watch for errors |
| 2 | max-age=604800 (1 week) | The full week |
| 3 | max-age=2592000 (30 days) | The full 30 days |
| 4 | max-age=31536000 (1 year) with includeSubDomains | The full year, then submit for preload |
The ramp follows hstspreload.org: 300 seconds, then a week, then a month, and you wait the full max-age at each stage, because a browser keeps enforcing the longest value it has seen.
Nginx
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
Put it in the HTTPS server block only. Browsers ignore the header over plain HTTP.
Apache
<IfModule mod_headers.c>
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
</IfModule>
Cloudflare
Under SSL/TLS and Edge Certificates, enable HTTP Strict Transport Security and choose the max-age, includeSubDomains and preload options. Cloudflare sends the header for you, so do not add it twice at the origin.
About preload
The preload list is built into browsers, so protection applies even on a first visit. To qualify you need max-age of at least one year, includeSubDomains and preload, and HTTPS everywhere. Removal is slow, since it depends on browser release cycles. Only submit when you are certain all current and future subdomains will support HTTPS.
Verify
Use the security headers checker to confirm the header is present with the values you set. Test in a private window and from curl: curl -sI https://example.com | grep -i strict.
If something breaks
Lower max-age to a small value and serve the header again. Browsers that have already seen a long value will keep enforcing it until it expires, but each visit refreshes the stored value to the new one. This is why the staged rollout matters.
Checklist before raising max-age
- No subdomain serves an invalid or expired certificate.
- Internal tools, mail web interfaces and old marketing subdomains were all reviewed.
- Certificate renewal has run successfully at least once without help.
- You can reach every environment over HTTPS, including staging.
Only raise the value when each item is true. A short delay at this stage is far cheaper than locking out a forgotten subdomain for a year.
Related reading: the SSL configuration checker and HTTPS best practices for SEO.
Frequently Asked Questions
Does HSTS help SEO?
Indirectly: it removes the HTTP-to-HTTPS redirect for returning users and prevents downgrade. It is not a direct ranking factor.
Can I use HSTS with a self-signed certificate?
Avoid it. Users cannot bypass certificate errors on HSTS sites.
Is the header needed on redirects?
Send it on the HTTPS responses. A redirect from HTTP does not carry it usefully.
Does HSTS work if a certificate expires?
No, and that is the danger. Once a browser has the header, it refuses to let visitors click past the warning, so renewal has to work before you raise max-age.
✅ Post-Installation Verification & SEO Checklist
After applying the steps above, run through this 5-point checklist to prevent browser warnings and protect your Google SEO rankings:
- Verify full CA chain trust and TLS 1.3 support using our Live SSL Checker.
- Scan for Mixed Content HTTP links and check HSTS/CSP headers on Security Headers Checker.
- Enforce a permanent 301 HTTP-to-HTTPS redirect with our HTTPS Redirect Generator.
- Confirm both root and
wwwversions resolve cleanly to one canonical HTTPS URL. - Verify your HTTPS property and
sitemap.xmlinside Google Search Console and Bing Webmaster Tools.