4

I would like to redirect admin subdirectory to a subdomain. I tried to create this rule for Nginx however it's not working:

location ^~ /admin/ {
        rewrite ^/admin(.*) http://admin.example.com$uri permanent;
     }

Thank you Regards

HTF
  • 3,148
  • 14
  • 52
  • 82

1 Answers1

8

Something like this should do the job:

location ^~ /admin/ {
    rewrite ^/admin/(.*) $scheme://admin.example.com/$1 permanent;
}
Vladimir Blaskov
  • 6,183
  • 1
  • 27
  • 22
  • 1
    Thanks for reply. I've changed a bit to optimize it: `rewrite ^ http://admin.example.com/login.php$1 permanent;`. I would like to also set another redirection from `http://admin.exmaple.com` to `http://admin.example.com/login.php`. Do I have to set another "location" or there is any better way to achive this. Will this cause any loops?. Thank you – HTF Jun 06 '12 at 12:29
  • 2
    You should use $scheme instead of http:// since you can later use https. `rewrite ^/admin/(.*) $scheme://admin.example.com/$1 permanent;` See: http://nginx.org/en/docs/varindex.html?_ga=1.117673482.204797739.1489752542 – darkless Mar 17 '17 at 12:10