Question

how-to-redirect-www-to-non-www-with-nginx-on-ubuntu-14-04

As i was following https://www.digitalocean.com/community/tutorials/how-to-redirect-www-to-non-www-with-nginx-on-ubuntu-14-04 i cant get to redirect from non www to the www. i still get the “Welcome to nginx!” when i dont put the www previous the domain.


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.

I once faced this on ubuntu 20.04 and I had already configured the SSL.

Add a server block inside the config file.

server {
server_name www.example.come;
return 301 $scheme://example.com$request_uri;

#Copy all the content of the original server block except server_name

}
Then remove the www domain from the original server under server_name. If you keep the www domain under the server_name section, then your app will keep redirecting. Then restart your Nginx service
sudo service nginx restart

Hi there @nlubkov,

Can you share the Nginx Server Blocks that you are currently using here?

Note that, if you want a redirect from non-www to www, you should only add the 301 rule in your non-www server block, that is where the redirection would be handled.

Your www server block should contain your correct document root and the rest of your configuration.

Regards, Bobby