Skip to content

Once your site works over HTTPS and visitors reach it through a redirect, there is still a small gap: the very first request can be made over plain HTTP, and a determined attacker on the same network could interfere with that single request. HTTP Strict Transport Security, usually shortened to HSTS, closes much of that gap by letting your site tell browsers, "from now on, only talk to me over HTTPS".

HSTS is simple to enable, which is exactly why it deserves caution. Browsers remember the instruction for the period you set, and they remember it even when you later change your mind. Used well, it is a strong hardening step. Used carelessly, it can make your site unreachable for the very visitors it was meant to protect. This guide explains what the header does, how to introduce it in stages, which settings need the most care and how to recover when something goes wrong. It assumes you already have a working redirect; if not, start with how to force an HTTP to HTTPS redirect correctly.

What HSTS actually does

HSTS is delivered as an HTTP response header named Strict-Transport-Security. A typical example is:

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

When a browser receives this header over a valid HTTPS connection, it stores a rule for your domain. For the number of seconds in max-age, the browser will:

  1. Turn any http:// request for your site into https:// before sending anything over the network.
  2. Treat certificate errors as hard failures. Normally a visitor can click "proceed anyway" on a warning page. With HSTS active, that option is removed, because a user who clicks through is exactly what an attacker hopes for.

Each time the browser receives the header again, it refreshes the timer, so an active site keeps its visitors protected continuously. The general behaviour of HTTP headers is defined in RFC 9110, HTTP Semantics, and the encryption layer that HSTS insists on is TLS, described in RFC 8446.

What a redirect cannot do

A redirect answers an insecure request after it has been sent. If an attacker sits between the visitor and your server for that first request, they can intercept it and never pass on your redirect. HSTS moves the decision into the browser: after the first secure visit, the browser upgrades the request locally, so no insecure request leaves the device.

Note the limit, though. HSTS cannot protect the very first visit from a browser that has never seen your site, because it has not yet received the header. Preloading, covered below, addresses that gap.

The directives

Directive Meaning Caution
max-age=N How long the browser remembers the rule Required. Start small, raise later
includeSubDomains Applies the rule to every subdomain Every subdomain must support valid HTTPS
preload Signals that you want the domain included in browser preload lists Hard to undo; requirements are strict

Seconds are easy to misjudge, so here are some reference values: 300 is five minutes, 86400 is one day, 604800 is one week, and 31536000 is one year.

Before you enable it: a checklist

  • Every page and resource works over HTTPS. Fix mixed content first. See what mixed content is and how to fix it for the process. HSTS does not create mixed content problems, but once visitors can no longer fall back to HTTP, you will notice them sooner.
  • Certificates are valid and monitored. If a certificate expires while HSTS is active, visitors cannot click past the warning. Put expiry alerts in place, as described in how to monitor SSL expiry automatically.
  • You know your subdomains. List them all: staging, mail, admin, legacy tools, test sites, regional sites. If you plan to use includeSubDomains, each must serve a valid certificate.
  • The redirect works and is a single hop. HSTS complements the redirect and does not replace it.
  • You can roll back. Know how to remove the header and what that does (and does not) undo.
  • TLS configuration is healthy. Handshake problems become more visible with HSTS. If you see failures, read what causes SSL handshake failures.

A staged rollout plan

Do not start with one year and every option. Increase gradually.

  1. Start with a tiny value. Send Strict-Transport-Security: max-age=300 for a few days. If something is wrong, the effect disappears within five minutes of your fix.
  2. Raise it to a day, then a week. Watch your error logs and support messages after each change. Look for visitors reporting certificate problems or inaccessible hosts.
  3. Test the subdomains. Before you add includeSubDomains, visit every subdomain over HTTPS and confirm that the certificate is valid for that exact name.
  4. Add includeSubDomains with a short value first. Return to a short period when you add it, then extend again.
  5. Raise to a long duration. Many sites aim for a year once everything is stable, but choose a period you could live with if you had to wait it out.
  6. Consider preloading last, and only if you truly want it. Preloading adds your domain to a list shipped with browsers, so even first-time visitors are upgraded. It usually requires a long max-age, includeSubDomains and the preload token, plus a working HTTPS redirect. Removal takes a long time to reach all users, because it depends on browser releases.

Where to add the header

Add the header at the layer that terminates TLS for visitors.

  • Web server. In Nginx, use an add_header directive with the always parameter inside the HTTPS server block. In Apache, use Header always set in the HTTPS virtual host.
  • CDN or load balancer. Many offer a setting to add HSTS. Use one place only; duplicates can result in two headers with different values.
  • Application framework. Some frameworks add it through middleware. Make sure it only sends the header on HTTPS responses.
  • Hosting panel or CMS. Some provide a switch. Check the value they use before you accept the default.

Remember that the header is ignored over plain HTTP. Do not add it to the HTTP-to-HTTPS redirect response and expect an effect; it must appear on valid HTTPS responses.

A worked example

A small company runs example.com and www.example.com on HTTPS. It also has status.example.com hosted by a third party and old.example.com, a legacy tool that only speaks HTTP.

The administrator reads about HSTS and sets max-age=31536000; includeSubDomains straight away. Within hours, staff report that the legacy tool cannot be opened. Browsers that visited the main site now upgrade old.example.com to HTTPS, and since it has no valid certificate, they refuse to connect, with no option to continue. The status page, hosted elsewhere, also fails for the same reason on one visitor group because its certificate name did not match.

The administrator removes includeSubDomains and reduces the value, but the browsers that already stored the one-year rule keep it until they receive a new header over HTTPS. Staff with the legacy tool must wait or clear the stored policy in their browser settings. The lesson: inventory subdomains first, start with a small max-age, and add includeSubDomains only when every host is ready.

A better plan would have been to move the legacy tool behind a valid certificate, confirm the third-party status page, run max-age=300 for a few days, and then raise values step by step.

Recovering when something goes wrong

  • Fix the cause first. Install a valid certificate or correct the name.
  • Send a short value. Serve max-age=0 over valid HTTPS to tell browsers to drop the rule, once they can connect again. Browsers only learn this when they successfully reach a page.
  • Clear the browser's stored policy. Browsers have internal pages or settings to remove HSTS entries for a single domain. This helps individual users but cannot reach everyone.
  • Preloaded domains are the hardest. If you are in a preload list, you must request removal and wait for browser releases to roll out. Plan as if it is permanent.

Limits and what to watch for

  • First visit remains unprotected unless you are preloaded, and preloading carries commitments.
  • Certificate problems become visible. With HSTS, visitors cannot click through errors, so expired or mismatched certificates immediately turn into outages.
  • Subdomains are included by includeSubDomains, including ones you forget. Internal tools with self-signed certificates will stop working in browsers that hold the rule.
  • Do not use HSTS on a domain you may need over HTTP. Development or test hosts on the same registered domain can be affected.
  • Mixed content still matters. HSTS does not repair pages that load http:// resources.
  • It is one layer. HSTS does not replace patching, strong TLS settings or good password practice. See Cloudflare's overview of HTTPS for the wider picture.
  • Do not copy values blindly. Choose a max-age you can tolerate if you have to wait for it to expire.

How to verify the header after enabling it

Do not assume the header is present because you added a line to a configuration file. Check it from the outside.

  1. Request your HTTPS address with a command-line client that prints response headers, and look for a single Strict-Transport-Security line with the value you intended.
  2. Request the HTTP address and confirm that the redirect still works and that the header is not relied upon there.
  3. Request a deep page, an error page and a static file. Some setups add headers only to certain locations, so a clean home page does not prove that the rest of the site is covered.
  4. Check the subdomains you intend to include, one by one, and confirm that each has a certificate matching its exact name.
  5. Open the site in a browser that has never visited it, then in one that has, and confirm that both behave normally. Browser developer tools often have a security or application panel that shows whether a rule is stored.

If you serve the site through more than one layer, such as a CDN in front of a web server, make sure only one layer adds the header. Two headers with different values can lead to unpredictable results, and it becomes hard to tell which one a given browser used.

Questions teams ask before enabling it

Do we need it on every site? Not necessarily. It is most valuable for sites that handle logins, payments or personal data, where an interrupted first request matters. A small brochure site benefits too, but the risk of a mistake is the same, so the care required is the same.

What if we use a CDN? A CDN may add the header for you or let you set it. Decide who owns the setting and write it down, so that a later change at the CDN does not silently override the web server.

Who should approve the change? Because the effect lasts as long as the period you set, treat it like a change that cannot be rolled back instantly. Ask whoever manages certificates and subdomains to review it, and schedule it for a time when someone can respond to reports.

Summary

HSTS lets your site tell browsers to use HTTPS only, upgrading insecure requests locally and removing the option to click through certificate errors. It strengthens a redirect but does not replace it, and it ignores any header sent over plain HTTP. Because browsers remember the rule, prepare first: fix mixed content, keep certificates valid and monitored, and inventory every subdomain. Roll out in stages from a very short max-age, add includeSubDomains only when every host is ready, and treat preloading as a last and nearly permanent step. If something breaks, repair the certificate, serve a short or zero value over valid HTTPS and expect some browsers to need time.

When you are ready to put a valid certificate behind your HSTS policy, the SSL certificates page explains the options.

Frequently asked questions

What does HSTS do?

HSTS, or HTTP Strict Transport Security, is a response header that tells a browser to contact your site only over HTTPS for a stated period. During that period the browser upgrades HTTP attempts itself and refuses to let the visitor click through certificate errors.

Is HSTS the same as an HTTP to HTTPS redirect?

No. A redirect is a server answer sent after an insecure request has already been made. HSTS is a browser-side memory that prevents later insecure requests from being made at all. They work best together.

Can HSTS lock visitors out of my site?

Yes. If you enable it and later let a certificate expire, serve a broken certificate or need HTTP on a subdomain, browsers that remember the policy will refuse to connect until the period ends. That is why a staged rollout with a short starting value is recommended.

Should I use includeSubDomains and preload?

Only when every subdomain, present and future, can serve valid HTTPS. includeSubDomains applies the rule to all of them. Preloading puts your domain in a list shipped with browsers, which is difficult to undo, so treat it as a final step.

Does the HSTS header work over plain HTTP?

No. Browsers ignore the header when it arrives over HTTP, because an attacker could forge it. It must be sent on valid HTTPS responses.

About the author

Bob Kanokphon Thongdee, founder of XPASS VPN

Bob Kanokphon Thongdee

  • Founder of XPASS VPN
  • Co-founder of eSIMonline.asia
  • More than 7 years in online marketing
  • Founder of Cojourney Visa and Cojourney Travel

Need an SSL certificate?

Pick the certificate type that fits your site and see the price before you check out.

SSL from $14.99

See SSL certificates

Keep reading

SSL Guides

How to Monitor SSL Certificate Expiry Automatically

Expired certificates still catch careful teams out. Learn how to read the expiry date, automate the check, route alerts and build a renewal routine that works.

10 min read

SSL Guides

What Causes an SSL Handshake Failure?

A failed SSL handshake means the browser and server could not agree on how to build a secure connection. Here are the usual causes and a calm way to find yours.

10 min read

SSL Guides

How to Force an HTTP to HTTPS Redirect Correctly

Installing a certificate is only half the job. Learn how to send every visitor to the HTTPS version with a clean, loop-free permanent redirect.

10 min read