Monday, October 6, 2014

Nginx redirect folder to subdomain



I'm trying to configure a redirect on Nginx from a folder to a subdomain.
I've tried already Nginx redirect: folder to external domain and Nginx rewrite rule (subdirectory to subdomain) and nginx: redirect subfolder to subdomain. Nothing works.



The problem, are simple. I have a Apache configured with this line:




RedirectPermanent /asd http://asd.domain.com/





Now, I'm trying to replicate this on a Nginx.
Every configuration I've tried, have the same problem:




If I go to http://www.domain.com/asd/, they redirect me to http://asd.domain.com/asd/.



If I go to http://www.domain.com/asd/index, they redirect to http://asd.domain.com/index.





I need to delete the sub-folder in all cases, for me, the first one are incorrect. How can I configure nginx for in first case, go to http://asd.domain.com/?



The rules are applied after root parameter. The config are:



server {
listen --
server_name --
root --
<> INSERT RULE HERE

access_log --
error_log --
include - (A file with php configurations).
}


Some examples of tried rules:




rewrite ^/asd/(.*) http://asd.domain.com/$1 permanent;




rewrite ^/asd(.*) http://asd.domain.com/$1 permanent;



rewrite ^/asd(.*) http://asd.domain.com$1 permanent;



location ^~ /asd/ { rewrite ^/asd/(.*) http://asd.domain.com/$1 permanent; }



location ^~ /asd/ { rewrite ^/asd(.*) http://asd.domain.com/$1 permanent; }



location ^~ /asd/ { rewrite ^/asd(.*) http://asd.domain.com$1 permanent; }




Also, location variation with "location ~ ^/asd/(.)" or "location ~ ^/asd(.)"




Thanks,


Answer



I think you were close with rewrite ^/asd/(.*) http://asd.domain.com/$1 permanent;



Per the documentation:





If a replacement string includes the new request arguments, the
previous request arguments are appended after them. If this is
undesired, putting a question mark at the end of a replacement string
avoids having them appended




But you might need to add a ?, and just for good practice that ending $ as such:



rewrite ^/asd/(.*)$ http://asd.domain.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...