NGINX www and non-www url standardization/canonicalization

I can’t seem to get url standardization to work with nginx, the service restarts fine, so i assume the code is sound just not doing what i want it to do. Does this look right for the server blocks? I’ve also tried return instead of rewrite.

server { listen 80; server_name; rewrite ^ $scheme://$request_uri permanent; } server { listen 80; server_name; root /usr/share/nginx/www/; index index.php index.html index.htm;


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

RESOLVED <br>i was stupid, it was a dns issue, for anyone else having this issue, you should check if your www. hostname is resolving at all, if it’s not, dns is probably not set up properly (specifically a c-name record), then to get the rest of the url’s to redirect, the server blocks with the redirect “command?” in it should take care of it (for later versions of nginx, earlier may require a “rewrite”)

Your 301 redirect www to non-www

<br>server { <br> listen 80; <br> server_name; <br> return 301 $scheme://$request_uri; <br>} <br> <br># Your Server <br>server { <br>listen 80; <br>server_name; <br>root /link/to/your/website/root; <br>index index.php index.html index.htm; <br>…etc <br>} <br>

ok, sorry, can you remove it then