Lesson 4 of 9 · 17m
HSTS and Transport: Make HTTPS Non-Negotiable
Strict-Transport-Security, preload mechanics, redirect design, and why the first visit problem never fully disappears without preload.
TLS failures in web applications are usually boring: one legacy subdomain still answers on port 80, one cookie lacks Secure, one redirect chain flashes plaintext before anyone notices. HSTS exists to make the browser remember that your origin speaks HTTPS only.
The header is simple; the commitment is not
Strict-Transport-Security: max-age=31536000; includeSubDomainsAfter one successful HTTPS response with this header, the browser refuses plaintext HTTP for that host for max-age seconds. Attempts to use http:// are internally upgraded before the network request. Certificate errors become hard failures — no convenient click-through.
includeSubDomains is powerful and dangerous. It covers every current and future subdomain, including the forgotten old-blog.example VM nobody owns anymore. Inventory subdomains before enabling it.
The first-visit gap and preload
HSTS is trust-on-first-use. A user whose first contact is over HTTP can still be intercepted before the browser ever sees the header. Preloading closes much of that gap by shipping the domain in browser lists:
Strict-Transport-Security: max-age=63072000; includeSubDomains; preloadThe preload ecosystem expects a long max-age, subdomain coverage, a valid HTTPS redirect, and continued availability. Submission is easy; removal is slow because browser release cycles are slow. Treat it as a multi-year operational commitment, not a security badge.
Redirect design still matters
http://example.com -> 301 https://example.com
http://www.example.com -> 301 https://example.com
https://www.example.com -> 301 https://example.comDo not redirect HTTP deep links to the bare home page; preserve path and query where appropriate. HSTS reduces exposure but does not make sloppy redirect chains harmless — each plaintext hop is still a first-contact opportunity on networks where the browser has no cached policy yet.
Cookies are part of transport security
HSTS does not mark cookies as secure. Set them correctly:
Set-Cookie: session=...; Secure; HttpOnly; SameSite=Lax; Path=/HSTS also does not replace TLS configuration. If the server negotiates obsolete protocol versions or serves the wrong certificate chain on mobile browsers, the browser will fail before your application gets a chance to help.
Field notes
- Never send HSTS over HTTP; browsers ignore it there. It must be on an HTTPS response.
- Test with a short
max-agesuch as 300 seconds before jumping to a year. - Internal hostnames can break when
includeSubDomainsreaches wildcard or split-horizon DNS names. - HSTS applies per host, not per port. It is not a general solution for non-HTTP services.
Was this lesson helpful?