开发者

.htaccess: Check file existence after RewriteRule

开发者 https://www.devze.com 2023-01-16 07:42 出处:网络
If have some rules in my .htaccess: RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond 开发者_开发知识库%{REQUEST_FILENAME} -l

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消