Search code examples
http-redirectnginxconfigurationregular-languagenginx-location

Nginx config: redirect


I'm having some difficulties figuring out my how to redirect in my nginx.config file.

I would like to redirect from:
http://domain.com/content/uploads/2016/06/img.jpg

to:
http://cdn.domain.com/uploads/2016/06/img.jpg

Add the "cdn." before the domain and cut out "content/".

I managed to add "cdn.":

location ~* ^/content/uploads/.*$ {
    try_files $uri $uri/ @cdn-domain;
}

location @cdn-domain{
    return 301 $scheme://cdn.domain.co.il$request_uri;
}

But how do I cut out "content/" ?


Solution

  • 302

    rewrite ^/content/uploads/(.*)$ http://cdn.domain.com/uploads/$1 redirect;
    

    301

    rewrite ^/content/uploads/(.*)$ http://cdn.domain.com/uploads/$1 permanent;
    

    For more: http://nginx.org/en/docs/http/ngx_http_rewrite_module.html