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 --
  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(.)"


  • 1
    given that the links you provided have suitable answers can you provide the configs that you have that dont work
    – Drifter104
    Jun 5, 2015 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. Jun 5, 2015 at 16:55

1 Answer 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. Jun 5, 2015 at 19:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .