On a VPS you choose one canonical address (for example https://yourdomain.tld) and send everything else there with a 301 redirect: HTTP to HTTPS, and www to the bare name (or the other way, if you prefer www). In nginx you do it with return 301; in Apache with Redirect permanent. It is all server configuration, no .htaccess involved.
Prerequisite: the certificate must already work for both names (with and without www). If only one has a certificate, a visitor going to the other sees the browser warning before reaching the redirect. See the certificate with certbot.
In nginx
| 1 |
A block for port 80 that forwards everything, keeping the path and parameters:server { listen 80; server_name yourdomain.tld www.yourdomain.tld; return 301 https://yourdomain.tld$request_uri; }
|
|
| 2 |
A block for www over HTTPS that sends to the bare name (it needs the certificate, even though it only forwards):server { listen 443 ssl; server_name www.yourdomain.tld; ssl_certificate /etc/letsencrypt/live/yourdomain.tld/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/yourdomain.tld/privkey.pem; return 301 https://yourdomain.tld$request_uri; }
|
|
| 3 |
The real site’s block keeps only server_name yourdomain.tld; and listen 443 ssl;.
|
|
| 4 |
Test and reload: sudo nginx -t and sudo systemctl reload nginx.
|
|
In Apache
| 1 |
The port-80 virtual host forwards everything:<VirtualHost *:80> ServerName yourdomain.tld ServerAlias www.yourdomain.tld Redirect permanent / https://yourdomain.tld/ </VirtualHost>
|
|
| 2 |
The www virtual host on 443 (with the same certificate lines as the site’s) ends with Redirect permanent / https://yourdomain.tld/.
|
|
| 3 |
Test and reload: sudo apache2ctl configtest (or apachectl on AlmaLinux and Rocky) and sudo systemctl reload apache2.
|
|
Checking it came out right
From your own computer: curl -I http://www.yourdomain.tld/page. The answer should be 301 and the Location: line should be the full final address, page included. Follow it with curl -I on the Location address until you see a 200. More than two hops means too many redirects.
| What you see |
Likely cause |
| “Too many redirects” (a loop) |
Something in front (Cloudflare in flexible mode, for example) talks HTTP to your server, and your server sends it to HTTPS again. See switching on SSL at Cloudflare without breaking the site. |
| www shows a certificate warning |
The certificate does not include the www name. Issue it for both. |
| The page loses its path |
You wrote the target without $request_uri (nginx) or without the trailing slash in Redirect (Apache). |
| It still opens over HTTP |
The port-80 block is not the one answering that name; check the server_name and whether the default site is still enabled. |
|
Use 301 only when you are sure. Browsers remember a 301 for a long time; a wrong target sticks to everyone who already visited. While you rehearse, use 302 (return 302 ...) and switch to 301 once it is right.
|
|
certbot can do part of this itself: the --redirect option (or the question it asks) adds the HTTP-to-HTTPS redirect. The www-to-bare one is yours to write. For redirecting pages and domains with Google in mind, see 301 redirects.
|
|
Did the site stop opening after you changed the configuration? If the server stopped answering on the network, we check that from our side.
Open a support ticket
|
RECOMMENDED PRODUCT Register your .ao domain Secure your company name before someone else registers it. from $33.30/yr Search a domain |