HTTP Strict Transport Security (HSTS)
The Strict-Transport-Security (HSTS) header tells the browser that the website should only be accessed using HTTPS, instead of using HTTP.
Why HSTS is Important
Even if you redirect all HTTP traffic to HTTPS on your server, the initial HTTP request is still vulnerable to a Man-in-the-Middle (MitM) attack, such as SSL stripping. An attacker can intercept the initial HTTP request and prevent the user from ever reaching the secure HTTPS version of your site.
By issuing the HSTS header, you instruct the browser to remember (for a specified period of time) that it should automatically convert any future HTTP requests to your site into HTTPS requests before they even leave the browser.
Best Practice Configuration
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
Let's break down these directives:
max-age=31536000: The time, in seconds, that the browser should remember that this site is only to be accessed using HTTPS. (31536000 seconds is 1 year).includeSubDomains: If this optional parameter is specified, this rule applies to all of the site's subdomains as well.preload: Indicates that the site owner consents to have their domain hardcoded into the browser's HSTS preload list. This ensures the browser connects via HTTPS even on the very first visit.
Warning
Do not include the preload directive unless you are absolutely certain that you can support HTTPS for your entire domain and all its subdomains indefinitely. Once you are on the preload list, it is difficult to be removed, and any subdomain without a valid SSL certificate will become inaccessible.