HTTPS Best Practices and HSTS
What you'll learn: How HTTP Strict Transport Security (HSTS) forces browsers to always use HTTPS and defends against downgrade attacks.
Even with TLS certificates properly configured, security vulnerabilities can arise from how browsers initially connect to your site. A user might type http://example.com (without the "s"), giving attackers a window to intercept that first request before redirection to HTTPS occurs—this is called a downgrade attack or SSL stripping.
What is HSTS?
HTTP Strict Transport Security (HSTS) is a security header that tells browsers: "For the next X seconds, only connect to me via HTTPS—don't even try HTTP." Once a browser receives this instruction, it automatically upgrades all future requests to HTTPS before sending them, eliminating the vulnerable HTTP connection entirely.
The header looks like this:
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
- max-age: How long (in seconds) the browser should remember this rule
- includeSubDomains: Apply the policy to all subdomains too
- preload: Eligible for browser preload lists (see below)
Protection Against Downgrade Attacks
Think of HSTS like a VIP list at a secure building. After your first visit through the proper entrance (HTTPS), your name goes on the list. From then on, security guards (the browser) won't let you use the unsecured side door (HTTP)—they automatically escort you to the secure entrance instead.
The Preload List
Browsers maintain HSTS preload lists—hardcoded databases of domains that should always use HTTPS, even on the very first visit. This eliminates the "trust on first use" problem entirely.
Key Takeaway: HSTS security headers instruct browsers to enforce HTTPS-only connections, preventing downgrade attacks by eliminating HTTP requests entirely—even before the first byte is sent.