Question

Redirect www to non www

How do I redirect http://www.example.com to http://example.com?

I am using LEMP, Nginx, Wordpress.

Qn 1: To which file should I add the below code to?

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

I have a few files in mind (I followed your tutorials)

  1. /etc/nginx/sites-available/wordpress
  2. /etc/nginx/sites-available/default

Qn 2: If I add the above code, must I still do the DNS part on CNAME (www and @)? Or can I skip this step?

Show comments

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.

The config looks good. Glad it’s working now! :]

Thanks FMCB and Kamal. I managed to get it to work. <br> <br>Steps I did were: <br>1) I added CNAME record (is this the DNS record you are referring to?) <br>2) I only added the 301 redirect to /etc/nginx/sites-available/default. <br>I left /etc/nginx/sites-available/wordpress untouched. <br>When I added the same code for both, I had an error. So I tried each files individually, and realised only the default file worked after I restarted nginx. <br> <br>this is my pastebin, please help me check if the code is ok. <br>http://pastebin.com/8s6n59m2 <br> <br>Thanks a lot!