Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to make the following kind of mapping from URL to file system for image files only:  ->  images/image.jpg  ->  images/folder/other.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?

share|improve this question
up vote 3 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]
share|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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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