How to add the HSTS header (Strict-Transport-Security)
HSTS is a response header that tells a browser: from now on, only talk to this site over HTTPS, for this long. Without it, the first visit (or any visit after the browser forgets) can start on plain HTTP, which is where an attacker on the same network can intercept or downgrade the connection.
Updated 2026-10-23
Why it matters
- It closes the gap between typing example.com and being redirected to https://example.com, which is the moment a downgrade attack works.
- Once a browser has seen the header, it refuses to load the site over HTTP at all until the time runs out.
- It costs one line of configuration and nothing at runtime.
How to fix it
- Add this header to every response from example.com.
Pick what your site runs on. Replace example.com with your own domain where it appears.
After you deploy, re-check the site. A CDN or the browser cache can keep serving the old headers for a few minutes.
nginx
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
Apache
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
Caddy
header {
Strict-Transport-Security "max-age=31536000; includeSubDomains"
}Cloudflare
Strict-Transport-Security: max-age=31536000; includeSubDomains
Vercel
{
"headers": [
{
"source": "/(.*)",
"headers": [
{
"key": "Strict-Transport-Security",
"value": "max-age=31536000; includeSubDomains"
}
]
}
]
}Netlify
/* Strict-Transport-Security: max-age=31536000; includeSubDomains
Next.js
const nextConfig = {
async headers() {
return [
{
source: "/(.*)",
headers: [
{ key: "Strict-Transport-Security", value: "max-age=31536000; includeSubDomains" },
],
},
];
},
};
export default nextConfig;Express
app.use((req, res, next) => {
res.setHeader("Strict-Transport-Security", "max-age=31536000; includeSubDomains");
next();
});Common mistakes
- Sending it before HTTPS works everywhere. With includeSubDomains, any subdomain that is still HTTP-only becomes unreachable in browsers that saw the header.
- Using a very short max-age. A value of a few minutes protects almost nothing. Use at least 15552000 (180 days), ideally 31536000 (one year).
- Adding the preload directive without reading the preload list rules. Getting onto the list is easy; getting off it takes months.
- Sending it over HTTP. Browsers ignore it there, so set it on HTTPS responses.
Check that it worked
Run your domain through the free security headers checker. It takes a few seconds, needs no account, and shows anything still missing.
Open the security headers checkerQuestions
- What value should I use?
- Strict-Transport-Security: max-age=31536000; includeSubDomains. Start with a short max-age such as 300 while you test, then raise it to a year once everything works over HTTPS.
- Do I still need the HTTP to HTTPS redirect?
- Yes. HSTS only takes effect after a browser has seen the header once over HTTPS, so the redirect handles that first visit.
- How do I check it worked?
- Run the domain through the free security headers checker, or open your browser's developer tools, look at any response on the Network tab, and find the Strict-Transport-Security header.
Related guides
Don't want to check by hand? Upzy watches your site around the clock and tells you when a header, certificate or email record slips. Start free.