Redirect all images in a directory to another website using .htaccess

5,038

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:
5,038

Related videos on Youtube

MathDav
Author by

MathDav

Updated on September 18, 2022

Comments

  • MathDav
    MathDav over 1 year

    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
    
  • MrWhite
    MrWhite almost 10 years
    @Martijn: Not sure why the edit added, what appears to be, a superfluous RewriteCond directive?
  • Martijn
    Martijn almost 10 years
    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
  • jafar pinjar
    jafar pinjar over 4 years
    its not working in my case