MENTECHLet’s talkالعربية
Menu

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

  1. Set up the redirect for your server below, and check that old links still reach the right page.
  2. Add the Strict-Transport-Security header with max-age=300, which is five minutes.
  3. 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 reload

If it shows an error, or the site stops, put the saved copy back and restart the server program at once.

Check that it worked

curl -sI http://example.com/some/page
curl -sI https://example.com/ | grep -i strict-transport

Good: 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.
Have us fix it for you
CallWhatsAppLet’s talk