Back to Guides
Troubleshooting

HSTS: Force HTTPS and Eliminate SSL Stripping

HTTP Strict Transport Security tells browsers to always use HTTPS, eliminating the vulnerable redirect that attackers can exploit.

12 min read
HSTS implementation guide for HTTP Strict Transport Security

What is HSTS?

HTTP Strict Transport Security (HSTS) is an HTTP response header that instructs browsers to only access a website using HTTPS, never HTTP. Once a browser receives an HSTS header, it will automatically convert any HTTP URLs to HTTPS before making requests.

Strict-Transport-Security: max-age=31536000; includeSubDomains; preload

This simple header provides powerful protection against man-in-the-middle attacks, specifically the "SSL stripping" attack that exploits the initial HTTP request before redirect.

The SSL Stripping Problem

When you type example.com into your browser (without https://), the browser first tries HTTP. The server then redirects you to HTTPS. This creates a vulnerability window:

The Attack Scenario

You connect to public WiFi at a coffee shop
You type bank.com in your browser
Browser sends HTTP request - attacker intercepts!
Attacker returns fake HTTP version of bank.com
You enter credentials on the fake site - stolen!

This attack is called SSL stripping because the attacker "strips" the SSL/TLS layer, keeping you on unencrypted HTTP while proxying your requests.

How HSTS Works

HSTS solves the SSL stripping problem by telling browsers to never make HTTP requests:

With HSTS Enabled

First visit: Browser goes to HTTPS, receives HSTS header
Browser caches the HSTS policy for max-age duration
Future visits: You type bank.com
Browser internally converts to HTTPS - no HTTP request ever sent!
Attack impossible - attacker never sees your traffic

HSTS Header Directives

DirectiveExampleDescription
max-age31536000How long (in seconds) to remember the HSTS policy. 31536000 = 1 year.
includeSubDomains(flag)Apply HSTS to all subdomains. Required for preload.
preload(flag)Indicates eligibility for browser preload list.

Common max-age Values

86400
1 day
2592000
30 days
31536000
1 year
63072000
2 years

The Preload List

Even with HSTS, there's still a vulnerability on the very first visit before the browser receives the HSTS header. The HSTS preload list solves this by hardcoding HSTS-enabled domains directly into browsers.

100,000+
Domains in list
0
First-visit vulnerability
hstspreload.org
Submit your domain

Preload is a Commitment

Once your domain is on the preload list, removal takes months. All subdomains must have valid HTTPS. Test thoroughly before submitting!

Implementation Guide

Apache (.htaccess or httpd.conf)

Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"

Nginx

add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;

IIS (web.config)

<system.webServer>
  <httpProtocol>
    <customHeaders>
      <add name="Strict-Transport-Security" 
           value="max-age=31536000; includeSubDomains; preload" />
    </customHeaders>
  </httpProtocol>
</system.webServer>

Express.js (Node.js)

app.use((req, res, next) => {
  res.setHeader('Strict-Transport-Security', 
    'max-age=31536000; includeSubDomains; preload');
  next();
});

Important: HSTS headers should only be sent over HTTPS connections. Browsers ignore HSTS headers received over HTTP.

Common Mistakes

MistakeProblemFix
max-age too shortPolicy expires, vulnerability returnsUse 1-2 years minimum
Missing includeSubDomainsSubdomains still vulnerableAdd after testing all subdomains
Premature preloadCan't easily undoTest thoroughly first
HTTP resourcesMixed content breaksAudit all resources
Forgetting subdomainsdev.example.com breaksPlan subdomain strategy

Testing HSTS

Using curl

curl -I https://example.com | grep -i strict-transport-security

Browser DevTools

Open DevTools → Network tab → Reload → Click main request → Headers tab → Look forStrict-Transport-Security

Check Preload Status

Visit hstspreload.org and enter your domain to check eligibility and current status.

Frequently Asked Questions

Does HSTS work without a valid certificate?

No. If your certificate expires or becomes invalid, users with cached HSTS policies will be completely locked out - browsers won't allow bypassing the certificate error. This is a feature, not a bug!

Can I disable HSTS after enabling it?

You can set max-age=0 to tell browsers to remove the policy, but they must visit your site again to receive this header. Preload list removal takes months.

Does HSTS affect SEO?

Not directly. However, HTTPS is a ranking signal, and HSTS ensures users always access the HTTPS version. It's generally good for SEO.

Should every site use HSTS?

If your site uses HTTPS (which it should), HSTS is strongly recommended. The only reason to avoid it is if you have legitimate HTTP-only services on subdomains.

Related Resources