HSTS: make browsers always use HTTPS
HSTS (HTTP Strict Transport Security) is a header that tells a visitor's browser to use only HTTPS for your site, for a set time. It closes the small gap where a first visit over http:// could be tampered with before the redirect.
Before you start
Turn HSTS on only when all of these are true.
- Your padlock works on
example.caandwww.example.ca. - Your site redirects HTTP to HTTPS. See Force HTTPS for your site.
- You've fixed mixed-content warnings. See Fix "not fully secure" mixed-content warnings.
- You're sure you will keep a valid certificate. A browser that has seen HSTS will not let visitors click past a certificate error.
Step 1: Start with a short test
Add the header with a five-minute lifetime. If anything breaks, it fixes itself quickly.
- Open Account → Hosting → your site → Files → File manager. Go to your site's folder, usually
public_html. - Edit
.htaccess. Make a copy first. See .htaccess basics with examples. - Add these lines.
<IfModule mod_headers.c>
Header always set Strict-Transport-Security "max-age=300"
</IfModule>
- Save. Load your site over HTTPS and check the header with your browser's developer tools. Open the Network tab, click the first request, and look in Response Headers.
strict-transport-security: max-age=300
Step 2: Raise the lifetime in stages
max-age is in seconds. Move up one step at a time and wait a few days between steps.
| Stage | max-age | Meaning |
|---|---|---|
| Test | 300 | 5 minutes |
| Week | 604800 | 7 days |
| Month | 2592000 | 30 days |
| Full | 31536000 | 1 year, the normal target |
Final form.
<IfModule mod_headers.c>
Header always set Strict-Transport-Security "max-age=31536000"
</IfModule>
Subdomains
Add includeSubDomains only if every subdomain you own, including ones you forgot, works over HTTPS.
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
A subdomain such as mail.example.ca or old.example.ca without a valid certificate becomes unreachable in browsers.
Preload: read this first
preload asks browser makers to ship your domain in a built-in list, so even the very first visit is forced to HTTPS.
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
Important: Preload is hard to undo. Removal from the list can take months, and until then your domain and every subdomain must work over HTTPS. Don't add preload unless you have a technical contact who has read the submission requirements, and you're certain every subdomain will always have a certificate. Most small business sites don't need it.
If something goes wrong
- Visitors see a certificate error with no "continue" option. HSTS is working as designed, but your certificate has a problem. Fix the certificate. See SSL errors.
- You need to turn HSTS off. Set
max-age=0and keep it live until visitors have loaded your site once. Browsers then forget the rule. - The header doesn't appear.
mod_headersmay not be active for that folder, or a caching plugin is serving old responses. Clear caches and check again. In WordPress, a security plugin may offer an HSTS switch instead. Use one place only, to avoid sending the header twice.
Still stuck? Ask us and we'll walk you through it.