webzod
account

Redirect www and apex to one host on nginx

headers.redirect.www_split · effort: minutes

Why it matters

Both names serve the site, so search engines see duplicate pages and logins, cookies and HSTS are split between two hosts. Pick one canonical host and permanently redirect the other to it.

The fix

  1. Remove www.example.com from the server_name of the main server, add the server below (the certificate must cover both names), run sudo nginx -t and reload.
nginx
server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name www.example.com;
    ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    return 301 https://example.com$request_uri;
}

The examples use example.com. Replace it with your own domain.

Read the documentation (opens in a new tab)

Scan your domain to see whether this applies to you.