How to add an HSTS header (and which max-age to use)
Last reviewed November 5, 2026 against the providers' current documentation.
The short version
Make sure http:// redirects to https://, then send Strict-Transport-Security: max-age=63072000; includeSubDomains (two years) on your HTTPS responses. If you're not sure every subdomain works over HTTPS, start with a short max-age such as 300 seconds and raise it in steps, or leave includeSubDomains off until you've checked. Don't add preload unless you mean to join the browsers' preload list: removal takes months.
Why it matters
HTTPS protects a connection only once it's made. When someone types your address or follows an old http:// link, the browser's first request can go out unencrypted, and on a hostile network (public Wi-Fi) an attacker can answer it before your redirect does. The Strict-Transport-Security header tells the browser to use HTTPS for your site for the next max-age seconds, so it upgrades those requests itself, and MDN notes that a certificate error on an HSTS site can't be clicked through. Two limits, both from MDN: browsers ignore the header when it arrives over plain HTTP, and it only works after the browser has made one secure visit and seen it. Closing that first-visit gap is what the preload list is for.
Fix it, in this order
See what your site sends now
Run our free headers check on your live https address. If it shows HTTPS is not enforced for returning visitors (no HSTS header), your host isn't sending it. From a terminal,curl -sI https://yourdomain.com | grep -i strict-transportshows the header if it's there, andcurl -sI http://yourdomain.comshould answer with a 301, 302, 307 or 308 redirect whoseLocationstarts withhttps://.Redirect HTTP to HTTPS first
HSTS only helps browsers that already reached your HTTPS site, so plain HTTP still has to redirect. MDN recommends a permanent redirect (such as 301) to thehttpsURL, and says the header belongs on the HTTPS response, because it's ignored over HTTP. Vercel's docs say it redirects every HTTP request to HTTPS with a 308 and that this can't be turned off. On Cloudflare, turn on Always Use HTTPS under SSL/TLS > Edge Certificates; Cloudflare advises against also redirecting at your origin, which can cause redirect loops. On nginx or Apache, add a redirect on the port 80 server. Netlify's HTTPS docs don't describe its redirect behaviour, so check yours with the curl command above.Pick max-age and check your subdomains
max-ageis in seconds, and every response that carries the header restarts the countdown. Two years (63072000) is what Vercel uses on custom domains and what hstspreload.org shows; one year (31536000) is Helmet's default and the preload minimum.includeSubDomainsapplies the rule to every subdomain, so first list yours (www,app,api,mail, old staging names) and make sure each one works over HTTPS. Cloudflare's docs warn that subdomains without HTTPS become inaccessible. If you're not sure, follow hstspreload.org's rollout: raisemax-agein stages and wait the fullmax-ageof each stage before the next one:
A mistake at 5 minutes costs 5 minutes; a mistake at two years can lock visitors out of a subdomain for two years.Strict-Transport-Security: max-age=300; includeSubDomains # 5 minutes Strict-Transport-Security: max-age=604800; includeSubDomains # 1 week Strict-Transport-Security: max-age=2592000; includeSubDomains # 1 month Strict-Transport-Security: max-age=63072000; includeSubDomains # 2 yearsAdd the header on your host
Send it on every HTTPS response:
Vercel already sends# Netlify, Cloudflare Pages or Workers static assets: _headers in your static folder (Vite: public/) /* Strict-Transport-Security: max-age=63072000; includeSubDomains # Vercel: vercel.json (Vercel already sends max-age=63072000 on custom domains) { "headers": [ { "source": "/(.*)", "headers": [ { "key": "Strict-Transport-Security", "value": "max-age=63072000; includeSubDomains" } ] } ] } # nginx: in the server block that listens on 443 (always = also on error pages) add_header Strict-Transport-Security "max-age=63072000; includeSubDomains" always; # Apache (mod_headers), in the HTTPS virtual host Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains" # Express (Replit and other Node servers): Helmet sends max-age=31536000; includeSubDomains by default app.use(helmet())max-age=63072000(withoutincludeSubDomains) on custom domains, so you only needvercel.jsonto change it; see our Vercel headers guide. On Netlify, the_headersfile has to end up in the publish directory (Netlify headers guide). On Cloudflare you can instead switch it on under SSL/TLS > Edge Certificates > Enable HSTS and choose a Max Age of 1 to 12 months, Apply HSTS policy to subdomains and Preload. Both Netlify and Cloudflare say their_headersfile isn't applied to pages generated by functions or Worker code (server-side rendering), so those responses must set the header in code. On nginx, a block with its ownadd_headerlines doesn't inherit the ones from the level above, so check that the header still appears on every page.Check it worked
Rerun the headers check or the curl command on a few pages, including one rendered by your server if you have any. The header should be on the https response; it doesn't need to be on the http redirect.Preload only when you're sure, and how to undo it
The preload list is built into browsers, so even a first visit uses HTTPS. hstspreload.org's requirements: a valid certificate, an HTTP-to-HTTPS redirect on the same host, HTTPS onwwwif it has a DNS record, and a header withmax-ageof at least31536000,includeSubDomainsandpreload, then you submit the domain on hstspreload.org. Netlify warns it's not easily reversible, and hstspreload.org says removal takes months to reach users with a browser update. To undo plain HTTPS-only (no preload), sendStrict-Transport-Security: max-age=0over HTTPS; MDN notes browsers forget the rule only after they see that response. On Cloudflare, set Max Age to 0 (Disable). Keep HTTPS working for the wholemax-ageyou've sent, or visitors who saw it can't reach the site.
Or ask your AI tool to do it
Paste this into Lovable, Bolt, Cursor, Claude Code or whichever tool built your app. Review what it changes before you deploy.
Add HTTP Strict Transport Security to my site. First check that every http:// URL redirects to the same https:// URL with a permanent redirect, and add that redirect if my hosting setup is responsible for it. Then send the header Strict-Transport-Security on every HTTPS response, including pages rendered by server code or functions. Use max-age=300; includeSubDomains for now (I'll raise it to 63072000 after checking every subdomain works over HTTPS), and do not add preload. Use the method that fits my host: a _headers file in the static folder that gets copied to the build output (Netlify, Cloudflare Pages, Cloudflare Workers static assets; for Vite that's public/), vercel.json headers on Vercel, add_header ... always in the nginx server block for port 443, Header always set in Apache, or helmet() in Express. List the subdomains you find referenced in my code and config so I can check each one serves HTTPS before I raise max-age.
How this happens
Nothing breaks without HSTS, so nobody adds it. Most hosts now redirect HTTP to HTTPS on their own, which makes a site look finished, but the redirect itself travels over plain HTTP. AI builders like Lovable, Bolt or Cursor generate the app, not the hosting config, so the header is missing unless the host sends it by default (Vercel does on custom domains; Netlify and Cloudflare leave it to you). It shows up as a finding only when a scanner or a customer's security review looks for it.
Check that it's really fixed
After you deploy, run the free check. It reads your live site the way an attacker would and tells you if any secret key is still visible.
Topic check: Security headers check
Find the other leaks before someone else does
The free check reads your home page and its scripts. The website security report goes through up to 30 pages and 40 JavaScript files, plus .env and .git files, security headers, cookies and CORS, domain and email records, and libraries with known vulnerabilities. Every finding comes with the fix for your host and a prompt for your AI tool.
$19 once, including 20 runs over 30 days so you can fix things and check again.

Questions
What max-age should I use for HSTS?
Two years (63072000) or one year (31536000) are the common values: Vercel's custom-domain default and hstspreload.org's example use two years, and one year is Helmet's default and the minimum for the preload list. Start shorter (300 seconds, then a week, then a month) if you haven't checked every subdomain. Our $19 report flags HSTS under 6 months as too short.
Is includeSubDomains safe to add?
Only once every subdomain works over HTTPS. With it, browsers refuse plain HTTP on all subdomains for the length of max-age, and Cloudflare warns subdomains without HTTPS become inaccessible. MDN's example shows the upside: a visitor tricked into an http://login. address is upgraded to HTTPS anyway.
Should I add preload?
Only if you want to submit the domain to hstspreload.org and are sure the domain and all its subdomains will stay HTTPS-only. It protects the very first visit, but removal takes months to reach users through browser updates. The preload word alone does nothing until you submit the domain.
Does Vercel, Netlify or Cloudflare send HSTS automatically?
Vercel does: its docs say custom domains get max-age=63072000 (without includeSubDomains) and .vercel.app addresses are preloaded. Netlify's docs show HSTS as a header you add in _headers or netlify.toml. On Cloudflare it's off until you enable it under SSL/TLS > Edge Certificates or send it yourself.
Why is HSTS ignored when I send it over HTTP?
Browsers ignore the header on plain HTTP responses, MDN says, so a man-in-the-middle can't add it for a site without HTTPS or shorten it. Send it on the HTTPS response and keep a redirect for http://.
I added HSTS and now a subdomain won't load. What do I do?
Make the subdomain serve HTTPS (a certificate from your host or Cloudflare), which fixes it immediately. If you need to roll HSTS back instead, send max-age=0 from the main domain over HTTPS; MDN says browsers drop the rule only after they receive that response, so visitors who don't come back keep it until it expires. If the domain is on the preload list, use hstspreload.org's removal form; it takes months.
Sources
- MDN: Strict-Transport-Security
- hstspreload.org: submission requirements, rollout and removal
- Vercel: Encryption and TLS (HTTPS redirect, default HSTS)
- Netlify: HTTPS (SSL) and HSTS preload
- Cloudflare: HTTP Strict Transport Security (HSTS)
- Cloudflare: Always Use HTTPS
- Cloudflare Pages: Headers (_headers file)
- Cloudflare Workers: Static assets headers
- nginx: add_header (always, inheritance)
- Apache: mod_headers (Header always set)
- Helmet: Strict-Transport-Security defaults