MaZZly
MaZZly

Reputation: 124

nginx redirect root domain and subpath differently

I'd like to have the root of my domain (www.domain.com) to redirect to a specific page on other domain.

And if someone goes to a subpath on my domain (www.domain.com/something) then it should redirect to the other domain with the same $request_uri.

I've tried the following configuration but somehow the latter 302 always triggers..

server {
    listen 80;
    server_name server_name ~^(?<subdomain>.+)\.domain\.com$ domain.com;
    location = / {
            return 302 https://www.otherdomain.com/special/something;
    }
    return 302 https://www.otherdomain.com/$request_uri;
}

My thinking is that maybe the latter 302 should be in a location block as well that has an exclusive match for /.. But I haven't managed to solve this problem myself.

Upvotes: 1

Views: 1422

Answers (1)

Siva Kumar
Siva Kumar

Reputation: 782

The following code solves your problem. (assumed that your nginx server name is www.domain.com). When you hit www.domain.com it will redirected to specific page and If you mention path then it wil redirected to that path on otherdomain server.

  if ( $request_uri = "/" ){
       return www.otherdomain.com/special/something;
       break;
   }
return www.otherdomain.com$request_uri;

Upvotes: 2

Related Questions