Monday, June 25, 2018

nginx: redirect subfolder to subdomain



I'm trying to redirect a subfolder to a subdomain in nginx. I want http://example.com/~meow/ to be redirected to http://meow.example.com/. I tried the following:



location ~ ^/\~meow/(.*)$ {
rewrite ^(.*)$ http://meow.example.com$1 permanent;

}


But it doesn't work. It gets caught by my location block for user directories:



location ~ ^/~([^/]+)/(.+\.php)$ {
[...]
}

and


location ~ ^/\~([^/]+)/(.*)$ {
[...]
}


So, how would I do this? Oh, and I only want /~meow/ to be redirected. All other user directories should not be changed.


Answer



You could try using nested location like this




location ~ ^/\~([^/]+)/(.*)$ {
location ~ ^/\~meow/(.*)$ {
rewrite ^(.*)$ http://meow.example.com$1 permanent;
}
[...]
}

No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...