Question

Redirecting non-www to www in Nginx

  • Posted on July 13, 2014
  • a.icbalAsked by a.icbal

I have successfully installed LEMP stack in Ubuntu 14 and running a phpBB forum. But the problem is that when I enter www.mydomain.com it shows a network error. Somebody please help me.


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.

Edit your virtualhost file and replace yourdomain.com with www.yourdomain.com and add this at the top:

server {
    listen 80;
    server_name yourdomain.com;

    return 301 http://www.yourdomain.com$request_uri;
}

Restart nginx:

sudo service nginx restart

Finally, add a CNAME record with the name of “www” and the hostname of “@”.

This comment has been deleted

Thanks Kamal Nasser. Now it is working perfect.