I have been working with apache for years and now wanted to move to nGinx keeping in view the performance everyone on internet seems to be talking about. But I its very frustrating for me for not being able to rewrite a non-www url to www i.e. example.com to www.example.com

I have referred to all the posts on StackOverflow and have tried their configurations. Somehow the system does not seem to work. I have wasted more than 2 hours in this and need help from you. My nGinx version is 1.2.1. This was 15sec work in Apache. I have somehow started missing apache.

2 Answers 2


Taken from my site www.yes-www.org:

server {
    listen [::]:80;
    server_name example.com;
    return 301 http://www.example.com$request_uri;

Your other server block should not respond to example.com, but only www.example.com.


do not worry...I could find a solution:

 if ($host = 'ukrugbyshop.com' ) {
         rewrite  ^/(.*)$  http://www.ukrugbyshop.com  permanent;
  • 1
    That is actually an inefficient solution as every single request to your site will be tested whether needed or not. Best solution is to create a separate server block with server_name ukrugbyshop.com and within that block, put return 301 http://www.ukrugbyshop.com$request_uri; In this way, requests with the www will not have to waste time going through the if test. See: superuser.com/questions/351940
    – Dayo
    Jul 14, 2012 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.