2

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:

http://example.com/tool/img/filename-with-special-chars.png

Should redirect to:

http://newdomain.com/img/filename-with-special-chars.png
0
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]
5
  • @Martijn: Not sure why the edit added, what appears to be, a superfluous RewriteCond directive?
    – MrWhite
    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?
    – MrWhite
    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
  • 1
    @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.)
    – MrWhite
    May 23 '14 at 21:48
  • its not working in my case Jan 10 '20 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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