0

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!

Luis Herranz
  • 358
  • 3
  • 12

1 Answers1

0

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;
}
Carsten
  • 17,991
  • 4
  • 48
  • 53
  • hey Bro, So I'm at this stage where I need the exact thing but 5% different I think. I want my users to hit ``http://embed.mydomain.com/embed.js`` and then I want the file on my S3 to be served. How do I do this please ? – Sahan Jun 13 '16 at 06:12