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.

Is there a way in nginx to redirect everything from domain1.com to domain2.com, except for the homepage?

Right now I have:

server {
    listen 80;
    server_name www.domain1.com domain1.com;
    rewrite ^ http://domain2.com$uri permanent;
}

This works, except that I'd like http://domain1.com (without any additional path) to be left alone and not redirected. Basically, I need to redirect everything, to avoid broken links, but I want to use the homepage of domain1 to serve a static file.

share|improve this question
add comment

1 Answer 1

This should do the trick.

server {
    listen 80;
    server_name www.domain1.com domain1.com;

    location = / {
            index static.file;   # CHANGE THIS
            root /path/to/root/; # CHANGE THIS
    }

    location / {
            rewrite ^ http://domain2.com$uri permanent;
    }
}
share|improve this answer
    
Thanks, but unfortunately, this didn't work for me. –  Alex Jun 18 '10 at 17:14
    
My apologies, I gave it some extra thoughts and edited it, this should show static.file only for location / (nothing!) and otherwise redirect, even if the static.file is called directly. –  Martin Fjordvald Jun 19 '10 at 12:21
add comment

Your Answer

 
discard

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