views:

24

answers:

1

I want to achieve my /assets/img urls are rewritten to a specific location when that file exists, but otherwise to a php script. Therefore I have these rules:

RewriteCond %{DOCUMENT_ROOT}assets/img$1/.$2.$4/$3.$4 -f
RewriteRule ^assets/img(.*)/([a-zA-Z0-9-_\.]*)\.([^/]+)\.([a-z]{2,4})$ assets/img$1/.$2.$4/$3.$4 [NC,QSA]
RewriteRule ^assets/img(.*)/([a-zA-Z0-9-_\.]*)\.([^/]+)\.([a-z]{2,4})$ assets/img/image.php?path=$1&file=$2.$4&key=$3 [NC,L,QSA]

Nevertheless, the RewriteCond and first RewriteRule don't seem to work. I always get pointed to the php script.

For example, I test this url but see the output of the php script

/assets/img/image.small.png

Then I test if the location of the first RewriteRule works, and see the image I want to have:

/assets/img/.image.png/small.png

My only conclusion is Apache cannot determine if the file exist. How can I make this happen?

+1  A: 

The problem is that a RewriteCond only applies to the rule immediately following it, so your second RewriteRule is always run. One solution is to negate the test you are making, then have the next command skip the two commands you want to run.

RewriteCond %{DOCUMENT_ROOT}assets/img$1/.$2.$4/$3.$4 !-f
RewriteRule . - [S=2]
RewriteRule ^assets/img(.*)/([a-zA-Z0-9-_\.]*)\.([^/]+)\.([a-z]{2,4})$ assets/img$1/.$2.$4/$3.$4 [NC,QSA]
RewriteRule ^assets/img(.*)/([a-zA-Z0-9-_\.]*)\.([^/]+)\.([a-z]{2,4})$ assets/img/image.php?path=$1&file=$2.$4&key=$3 [NC,L,QSA]
Conspicuous Compiler
Adding a skip flag (skipping 1 rule) to Jurian's first RewriteRule should be sufficient, assuming I understand his intent correctly.
outis
Thanks for the answer, this is really helpful! Unfortunately I notice now the first rule (rewriting to another location of the image) does not work at all (eg when the condition and second rule are removed). Still, a direct request (see question) works fine. There is more wrong than just the skip flag?
Jurian Sluiman