0

How can I redirect all requests trying to reach a folder?

for example, I would like to redirect:

somedomain.com/folder/subfolder/index.html
somedomain.com/folder/subfolder2/something.html
somedomain.com/folder/subfolder3/somethingelse.html

to

somedomain2.com/index.html

what I've tried:

if ( $request_uri = "/folder/.*" ) {
  rewrite ^/(.*)$  http://domain2.com/embed.html    permanent;
}
2
server {
    server_name somedomain.com;

    location /folder/ {
        return 301 $scheme://somedomain2.com;
    }
}

server {
    server_name somedomain2.com;

    location / {
        #index index.html; # You could wish to add that, if index default value does not suits your needs
    }
}

That will issue a permanent redirect for any /folder/* request.

I took the liberty of mimicking the scheme being used with somedomain for somedomain2 with the $scheme variable.

I have not forced the /index.html URI in the redirect. You may wish to do it. I also relied on the default value of the index directive, which is maybe not right for you.

If you wished to handle the content from the other server transparently, you could use proxy_pass to proxy the request to it.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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