Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rewrite all url to another url except one in nginx

I want redirect

https://dev.abc.com/ to https://uat.abc.com/

https://dev.abc.com/first to https://uat.abc.com/first

https://dev.abc.com/second to https://uat.abc.com/

https://dev.abc.com/third/ to https://dev.abc.com/third/ (Point the same)

I have tried with following config and achieved first three. But last one also redirecting to uat. Can anyone help me in this situation.

server {
        listen 80;
        server_name dev.abc.com;
        root /var/www/

      location ~* ^/first{
      return 301 https://uat.abc.com$request_uri;
      }

      location ~* ^/second{
      return 301 https://uat.abc.com;
      }

      location ~* ^/{
      return 301 https://uat.abc.com$request_uri;
      }

Can anyone help me on this configuration?

like image 431
Rathna Kumar Avatar asked Sep 17 '25 04:09

Rathna Kumar


1 Answers

location ~* ^/ matches any URI that begins with / - which is any URI that hasn't already matched an earlier regular expression location rule.

To match only the URI / and nothing else, use the $ operator:

location ~* ^/$ { ... }

Or even better, and exact match location block:

location = / { ... }

See this document for more.

like image 93
Richard Smith Avatar answered Sep 19 '25 18:09

Richard Smith