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'm trying to rewrite a particular url, but it doesn't seem to work unless a subdomain is included as part of the url. For example, given the following:

server {
  server_name .example.com ;
  listen  80  default;
  root  /usr/share/nginx/www/example/public;
  rewrite ^ /test redirect;
}

http://www.example.com/original correctly redirects to http://www.example.com/test. But http://example.com/original does not?

I'm running nginx 1.0.11 (custom built) on a Fedora box if that helps.

EDIT: PS - I've also tried server_name example.com *.example.com with no luck.

share|improve this question
    
Increase your error_log verbosity to notice, and turn on rewrite_log. See if anything relevant shows up in the error logs, and post the relevant sections here. (You can also add the same directives to the http block to see that the error is not originating elsewhere). –  cyberx86 Jan 4 '12 at 4:29
    
You probably have another server block interfering, please paste all your server blocks. –  Martin Fjordvald Jan 4 '12 at 7:46
    
Wow, I'm a total moron. Sorry for all the noise. Basically, I edit /etc/hosts so that I can type "www.example.com" into my browser and have it point to 127.0.0.1 rather than the 'real' "www.example.com". I do this so I can write things like an nginx config with ".example.com" rather than having to type "localhost" on one machine and then ".example.com" on the production server. The only problem is that I didn't add example.com to /etc/hosts! So it was pointing to the 'real' server which I was obviously not changing. Thanks for all the help. –  ktr Jan 4 '12 at 17:05
add comment

1 Answer

up vote 0 down vote accepted

You can try redirecting any request to www.example.com go to example.com:

server {
    server_name www.example.com;
    listen 80 default;
    rewrite ^/(.*) http://example.com/$1 permanent;
}

server {
    server_name example.com;
    location /original/ {
        rewrite ^/(.*) http://example.com/test redirect;
    }
    # rest of config here;
}
share|improve this answer
    
Thanks, but still no luck. The 1st rewrite happens as expected (www.example.com -> example.com), but rewrites inside of the example.com directive still don't seem to work ... –  ktr Jan 4 '12 at 4:11
    
Made a change in the example.com rewrite block. And forgot to add, the root line is not needed in the www block. –  111110100 Jan 4 '12 at 7:20
    
Does the above example work on your machine? I.e., just wondering if you tried it out ... it still doesn't seem to redirect anything (even with the location directive) on my machine. Maybe I'll try it out on another machine and see if I have the same problem. Thanks for the suggestions though. –  ktr Jan 4 '12 at 16:45
    
@ktr the above example is the same one I use on production servers. Redirect the www to the none-www domain. –  111110100 Jan 5 '12 at 6:41
    
thanks for the help, got it working :) –  ktr Jan 5 '12 at 17:59
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.