Redirects, WWW, Non-WWW and HTTPS Rules in NGINX
Redirects are important because they keep your website consistent. Without proper redirects, visitors may reach different versions of the same site, such as http://example.com, https://example.com, http://www.example.com, and https://www.example.com. Search engines may treat these as separate URLs unless your NGINX rules send everyone to one preferred version.
Step 1: Choose your preferred domain format. For example:
or
Step 2: Make sure DNS points both the root domain and www record to your server.
Step 3: Configure NGINX so HTTP redirects to HTTPS.
Step 4: Configure either www to non-www, or non-www to www.
Step 5: Test the configuration:
sudo nginx -t
Step 6: Reload NGINX:
sudo systemctl reload nginx
Useful testing commands:
curl -I http://example.com curl -I http://www.example.com curl -I https://example.com curl -I https://www.example.com
Look for 301 or 308 redirect responses and confirm the final location is correct.
A common mistake is creating redirect loops. This happens when rules bounce visitors between versions repeatedly. If your browser says too many redirects, review the order of your server blocks and SSL redirect rules.
For SEO, consistency matters. Pick one final domain format, use it everywhere, and make all other variations redirect to it. This gives users a cleaner experience and helps avoid duplicate content problems.