views:

46

answers:

1

If have some rules in my .htaccess:

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^.*$ - [NC,L]

RewriteRule ^assets/img(.*)/([a-zA-Z0-9-_\.]*)\.(.*)\.([a-z]{2,4})$ /assets/img$1/.$2.$4/$3.$4 [NC,L]

This causes /assets/img/path/image.jpg > /assets/img/path/image.jpg

The second rule: /assets/img/path/image.small.jpg > /assets/img/path/.image.jpg/small.jpg

Now I want to check whether small.jpg exists, so I replaced the last rule by:

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

But that doesn't work. Any ideas how check existence of a file if it's already rewritten by another RewriteRule?

A: 

Do it the other way round: Only rewrite the request to the destination if it actually exists:

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]
RewriteRule ^assets/img(.*)/([a-zA-Z0-9-_\.]*)\.(.*)\.([a-z]{2,4})$ /assets/img/image.php?path=$1&file=$2.$4&template=$4 [NC,L,QSA]

By the way: Don’t use .* if you can be more specific. In this case [^/]+ could fit to only match a path segment.

Gumbo