Question

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

  • Posted August 13, 2020
  • Nginx

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.

Subscribe
Share

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

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.

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