Search code examples
nginxurl-rewritingvhosts

How to redirect part of url to new domain by nginx


I want to redirect part of old url to new url by nginx.

https://server1.com/app1/app2 to https://server2.com/app2

https://server1.com/app1/app2/link1 to https://server2.com/app2/link1

https://server1.com/app1/app2/link2?param1=value1 to https://server2.com/app2/link2?param1=value1

So part of previous server needs to be redirect to server2.

I tried: rewrite ^(/app1/app2.*) https://server2.com$1 permanent;

Result: https://server2.com/app1/app2 instead of https://server2.com/app2

How can I manage redirect only url of app2 to new server?


Solution

  • Your capturing group takes all the original url and passes it as is to server2. I suggest you to capture only the part after app2 and redirect this group to server2.com/app2.

    rewrite ^/app1/app2(.*) https://server2.com/app2$1 permanent;