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?

link|improve this question
feedback

1 Answer

up vote 2 down vote accepted

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]
link|improve this answer
I would suggest the QSA flag could likely be removed as it's unlikely you'll have a query string following the image URI. – Jeremy Bouse Sep 3 '09 at 19:49
Thanks, it works that way! Also, the QSA flag was unnecessary. – Joonas Pulakka Sep 4 '09 at 3:55
feedback

Your Answer

 
or
required, but never shown

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