RewriteCond总是验证为false

时间:2010-10-27 10:59:17

标签: .htaccess mod-rewrite

我的RewriteRule中有一个正则表达式,目前工作正常。其次,我想测试文件(请求被重写的位置)存在。因此,我在我的规则之前放置了一个RewriteCond,但不幸的是,它总是被证实为假。

这是我的正则表达式:

assets/img(.*)/([a-zA-Z0-9-_\.]*)\.([^/]+)\.([a-z]{2,4})

我创建了这个.htaccess:

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

没有RewriteCond的请求,其中存在重写的位置,正常工作。对于那些感兴趣的人,我的完整.htaccess发布在这里:http://pastie.org/1293667

1 个答案:

答案 0 :(得分:1)

问题是RewriteCond仅适用于紧随其后的规则,因此您的第二个RewriteRule始终会运行。一个解决方案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]