Webmasters Stack Exchange is a question and answer site for pro webmasters. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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? – w3dk May 19 '14 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? – w3dk May 19 '14 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 '14 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.) – w3dk May 23 '14 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.