i have setup the following nginx rewrite rule . But doesn't seems to be working,


location /images {
       rewrite ^(.*)$1 http://sub.domain.com/$1 permanent;


for images i'm trying to redirect to another domain. How could i make this work ?

1 Answer 1


You don't need regex capture here. Try this:

location /images {
    rewrite ^ http://sub.domain.com$request_uri permanent;
  • This one redirects, but after php prcessed the request. Php code redirects to get default.image if images not found and this default image being requested from sub.domain with the above rule. So the redirection should be somehow applied before php processing i guess... Apr 8, 2014 at 9:12
  • Please post the full configuration of your server. Please also give more details on what you actually want from the setup. Your comment implies that there is something else going on here. Apr 8, 2014 at 12:31
  • i havent got anything special in nginx conf. As a fact when i request domain.com/image/my.img it redirects to sub.domain.com/image/my.img. But when i browse to the web page embeded with the image i get image not available(default image). I tried location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ { rewrite ^(.*) sub.domain.com/$1 break; } Didnt work either Apr 8, 2014 at 15:04
  • Is the URL to the image correct in your web page? The break directive makes an internal redirect, and doesn't work at all the way you wrote it. If you want to make a browser redirect, you have to use permanent directive like in my example. Apr 8, 2014 at 15:53

You must log in to answer this question.

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