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?


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]
  • 1
    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

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.