Force HTTPS with HSTS in .htaccess

Published

Updated

Enforce secure connections always.

fortrabbit redirects every http:// request to https://. The HSTS header goes one step further: browsers that saw it switch to HTTPS on their own and never send an unencrypted request to the domain.

Set the header

Add this to the .htaccess in the root path:

<IfModule mod_headers.c>
  Header always set Strict-Transport-Security "max-age=31536000"
</IfModule>
apache
.htaccess

max-age is in seconds; 31536000 is one year. The platform's HTTP to HTTPS redirect still answers the very first request, before the browser has seen the header. With HSTS, later requests skip that redirect and a downgrade attack has no unencrypted request to intercept. See HTTP Strict Transport Security.

What it locks in

A browser that saw the header uses HTTPS for the domain until max-age runs out, even when the site later serves http:// again. Removing the header does not undo that for visitors who already have it.

OptionEffect
max-age=300Five minutes; a safe value for a first test
includeSubDomainsAlso covers every subdomain, including ones on other hosts without HTTPS
preloadAsks to be added to the browsers' built-in list, see hstspreload.org; removal takes months

Start with a short max-age, raise it once all pages work over HTTPS, and add includeSubDomains only when every subdomain has a certificate. More HTTP headers work the same way.

Written by humans and AI in collaboration.

AI use & editorial processEdit on GitHub ↗