简体   繁体   中英

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM