0

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,

  • 1
    given that the links you provided have suitable answers can you provide the configs that you have that dont work – Drifter104 Jun 5 '15 at 16:34
  • I've included some configurations I've tried to do. Note, the problem are ONLY on the first case, when I go to www.domain.com/asd/. There's no problem when I go to a page inside the folder. – Sakura Kinomoto Jun 5 '15 at 16:55
1

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;
  • Still fail on Firefox, but, I've tried with iE and these solution works correctly. I think the problem are now on firefox cache. Thanks. – Sakura Kinomoto Jun 5 '15 at 19:04

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.