Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question

migrated from stackoverflow.com Jul 16 '12 at 16:39

This question came from our site for professional and enthusiast programmers.

add comment

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.

share|improve this answer
add comment

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

 if ($host = 'ukrugbyshop.com' ) {
         rewrite  ^/(.*)$  http://www.ukrugbyshop.com  permanent;
      }
share|improve this answer
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 '12 at 13:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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