Rewrite only images with .htaccess

.htaccessmod-rewrite

I'm trying to make the following kind of mapping from URL to file system for image files only:

mysite.com/image.jpg  ->  images/image.jpg
mysite.com/folder/other.jpg  ->  images/folder/other.jpg
mysite.com/x/y/z.jpg  ->  images/x/y/z.jpg

I've tried the following, but it apparently causes an infinite redirect loop:

RewriteRule ^(.*\.(gif|jpg|png))$ images/$1 [QSA,L]

Any mod_rewrite gurus here?

Best Answer

Just add a condition before your rule to see if it has already been rewritten to images/

RewriteCond %{REQUEST_URI} !/images/.*
RewriteRule ^(.*\.(gif|jpg|png))$ images/$1 [QSA,L]
Related Topic