Send every visitor to the secure address
Always secure.
A visitor who types your address without https can be watched or misdirected on a public network. A redirect moves everyone to the secure address, and one header makes browsers remember it.
Before you start
- Whoever runs your server or hosting can do this. Send them this page.
- Copy the settings file to another folder, such as your home folder, before you change it. Putting it back is the way to undo this.
- Make sure the certificate renews by itself first. After the second part, an expired certificate leaves visitors no way past the warning.
The order
- Set up the redirect for your server below, and check that old links still reach the right page.
- Add the Strict-Transport-Security header with max-age=300, which is five minutes.
- If nothing breaks, raise it step by step: 604800 for a week, 2592000 for a month, then 31536000 for a year. Wait the full time at each step.
The steps
Put the first part in place of the site's current block for port 80. If one block handles both ports, delete its listen 80 lines. The last line goes in the block for port 443, with a copy of any add_header lines from nginx.conf, or they stop working. Repeat it in any location block that has its own.
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}
add_header Strict-Transport-Security "max-age=300" always;Then check the settings and reload.
sudo nginx -t && sudo nginx -s reloadIf it shows an error, or the site stops, put the saved copy back and restart the server program at once.
The first part is the site for port 80. The last line goes in the site for port 443. On Debian and Ubuntu, first run: sudo a2enmod headers
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
Redirect permanent "/" "https://example.com/"
</VirtualHost>
Header always set Strict-Transport-Security "max-age=300"Then check the settings and reload.
sudo apachectl configtest && sudo apachectl gracefulIf it shows an error, or the site stops, put the saved copy back and restart the server program at once.
Both lines go in the frontend that listens on ports 80 and 443.
http-request redirect scheme https code 301 unless { ssl_fc }
http-after-response set-header Strict-Transport-Security "max-age=300" if { ssl_fc }Then check the settings and reload.
sudo haproxy -c -f /etc/haproxy/haproxy.cfg && sudo systemctl reload haproxyIf it shows an error, or the site stops, put the saved copy back and restart the server program at once.
The redirect is automatic. Add this line to the site.
header Strict-Transport-Security "max-age=300"Then check the settings and reload.
sudo systemctl reload caddyIf it shows an error, or the site stops, put the saved copy back and restart the server program at once.
On Windows Server 2019 or newer, select the site in IIS Manager and choose HSTS on the right. Tick Enable and Redirect Http to Https, and set Max-Age to 300.
First install a valid certificate on your own server. A free Cloudflare Origin certificate is enough. Then set the encryption mode to Full (strict), and switch on Always Use HTTPS under SSL/TLS, Edge Certificates. The header is on the same page, and its shortest setting is one month.
CloudFront: set Viewer protocol policy to Redirect HTTP to HTTPS in every behaviour. Load balancer: give the port 80 listener a redirect to HTTPS, port 443, code 301. The header is under the HTTPS listener's Attributes.
Open Domains and switch on Force HTTPS Redirect. Remove any other redirect, or the two may chase each other.
Open SSL/TLS Certificates and switch on Redirect from http to https. Switch on HSTS there later, without subdomains.
Check that it worked
curl -sI http://example.com/some/page
curl -sI https://example.com/ | grep -i strict-transportGood: code 301 or 308, and a Location that starts with https and ends with the same page. The second command shows the header.
To undo it
The redirect: remove it and reload, but keep the secure site working, because browsers may remember it. The header: send max-age=0 and keep the secure site working. Deleting the line is not enough, because browsers remember it.
Common mistakes
- An endless redirect behind Cloudflare or a load balancer. The server sees the visit as not secure and redirects forever. Redirect in one place only.
- Sending every old link to the home page instead of the same page.
- Adding includeSubDomains while an old subdomain has no certificate. It becomes unreachable.
- Adding preload. Its own maintainers no longer recommend it, and leaving takes months.