What You'll Learn
You'll learn how to configure the Strict-Transport-Security header to force browsers to always use HTTPS and prevent downgrade attacks.
Why HSTS Matters
Even after you've upgraded all your resources to HTTPS, users might still type http://yoursite.com in their browser or follow an old HTTP link. That initial HTTP request is vulnerable—an attacker on the network could intercept it before the server redirects to HTTPS. This is called a downgrade attack or SSL stripping.
Think of HSTS like putting your site on a permanent "HTTPS-only" list inside the user's browser. Once set, the browser automatically converts any HTTP requests to HTTPS before they even leave the device—no network round-trip needed.
How to Configure HSTS
Set the Strict-Transport-Security header in your server responses:
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
Breaking it down:
max-age=31536000: Tells browsers to remember this setting for one year (in seconds)includeSubDomains: Applies HSTS to all subdomains toopreload: Opt-in for inclusion in browsers' hardcoded HSTS lists (requires separate submission to hstspreload.org)
The Protection Flow
- First visit: Browser receives HSTS header, remembers your site requires HTTPS
- Future visits: Browser automatically upgrades
http://tohttps://locally - No network request happens over HTTP—the attack window is closed
Start with a shorter max-age during testing (e.g., 300 seconds), then increase to a year once confident. Remove includeSubDomains if you have non-HTTPS subdomains.
Key Takeaway: The Strict-Transport-Security header makes browsers enforce HTTPS automatically for your domain, eliminating the vulnerability window of that first HTTP request and protecting users from downgrade attacks.