Question

I'm still quite new to nginx and despite I took a look at all the answers of this kind of questions and tried everything they said, nothing has worked so far.

I want to do this redirection:

From http://www.mydomain.com/folder to http://cdn.mydomain.com/folder

I have put this inside the server conf:

    location /folder/ {
       rewrite ^ http://cdn.mydomain.com/folder/$request_uri permanent;
    }

I think I am near but still don't get where I am wrong.

In my old server, using this .htaccess inside each folder works fine:

    RewriteEngine On
    RewriteRule (.*) http://cdn.mydomain.com/folder/$1 [L,R=301]

Thanks in advance!

Was it helpful?

Solution

You're currently matching only the exact location /folder/. You should edit that to match all locations beginning with /folder/:

location ^~ /folder/ {

Also, your rewrite has one and a half errors. If I request http://www.mydomain.com/folder/123.png, then you will redirect me to http://cdn.mydomain.com/folder//folder/123.png, which is most probably not correct. Below is the complete location and rewrite directive. The question mark after $request_uri makes sure that a potential query string only gets added once.

location ^~ /folder/ {
   rewrite ^ http://cdn.mydomain.com$request_uri? permanent;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top