Nginx – Redirect everything except the homepage

nginxredirect

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.

Best Answer

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;
    }
}
Related Topic