Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I've got a set of rewrites that is supposed to catch any requests for /cache/images/* that doesn't exist on the file system and return a generic image according to a suffix (a corresponding *_1.jpg for requests for assets like /cache/images/foo_1.jpg that don't exist).

The code chunk below works for most cases but is also matching all requests that end in 1.jpg, 2.jpg or 3.jpg, such that "domain.com/1.jpg" is also returning the /images/generic_1.jpg generic image.

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule . - [L]

RewriteCond %{REQUEST_URI} ^/cache/images/.*$ [NC]
RewriteRule .*1.jpg /images/generic_1.jpg [L]
RewriteRule .*2.jpg /images/generic_2.jpg [L]
RewriteRule .*3.jpg /images/generic_3.jpg [L]

If I copy the second RewriteCond in front of the 2.jpg and 3.jpg RewriteRules, it works as designed but I'd like to do this properly and have the RewriteCond apply to the three RewriteRules below it. What am I doing wrong?

share|improve this question
    
Is it possible that you can't have multiple RewriteRules apply to a single RewriteCond? I know that typically you apply multiple RewriteConds to a single RewriteRule. –  editor Feb 29 '12 at 18:34
add comment

1 Answer

[L] - means last rule to evaluate: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewriterule (-: Can you take it from here ... ?

Edit 1 - Cleaning up the Rules:

If your files are named in this pattern, you could do something like this:

RewriteRule .*(\d{1,}).jpg /images/generic_$1.jpg

\d{1,} - matches a digit 1 digit or larger; put this in a pair of "()" means "save the matched string to $1" (aka first match), which you can then use in the destination path.

share|improve this answer
    
Interesting! Thanks. I thought it was the last rule to evaluate only if it matched? Sort of a "thus far but no further" thing to break out of the current RewriteCond if you find a match. –  editor Feb 29 '12 at 18:46
    
"thus far but no further" -- sums it up nicely. –  KM. Feb 29 '12 at 18:49
    
Unfortunately, so far removing the [L] flags is not solving the problem. –  editor Feb 29 '12 at 18:52
    
See Edit 1 above –  KM. Feb 29 '12 at 19:13
add comment

Your Answer

 
discard

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.