Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

How can I redirect all .png and .jpg image URLs within the /tool/img directory to another website? Filenames can consist of special (unicode) characters.

For example, the following URL:


Should redirect to:

share|improve this question

1 Answer 1

Try something like this:

RewriteCond %{REQUEST_URI} ^/tool/img/ [NC]
RewriteCond %{REQUEST_URI} \.(jpg|png)$ [NC]
RewriteRule ^/tool/img/(.*)$ http://newdomain.com/img/$1  [NC,U,QSA]
share|improve this answer
@Martijn: Not sure why the edit added, what appears to be, a superfluous RewriteCond directive? –  w3d May 19 at 11:46
I suspect this is non-Apache? On Apache, the RewriteRule pattern should start ^tool/img/ in per-directory .htaccess files - the directory prefix (ie. / in this case) is removed in the pattern matching. Also, I don't think U is a valid flag on Apache? –  w3d May 19 at 11:50
I made the image extentions together. This way it's very easy to expand and read (you notice they're all images in a split second). I made the first RewriteCond to make a fast, easy readable check for requested uri before we start more complex conditions. The / is correct, thats my bad. I didnt add the U flag though. Ill add comments to my edit –  Martijn May 19 at 11:57
@Martijn: But it is "faster" to check the URI path in the RewriteRule directive, rather than RewriteCond, and you certainly wouldn't include both. (The RewriteRule pattern is checked before the RewriteCond directives are processed.) –  w3d May 23 at 21:48

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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