无论是否找到文件,RewriteCond
的行为都会受到影响。
RewriteEngine On
RewriteCond %{REQUEST_URI} !/images
RewriteCond %{REQUEST_URI} !/index.php
RewriteRule ^(.*)$ /index.php?$1 [NC,L,QSA]
如果存在图像(或js,css)文件,它将加载正常。如果文件不存在,则/index.php?$1规则匹配而不是仅显示404。
http://example.com/images/file_exists.jpg = /images/file_exists.jpg [确定]
http://example.com/images/file_does_not_exist.jpg = /index.php?/images/file_does_not_exist.jpg [Not OK]
注意 - 我在共享主机上并意识到他们可能正在对REQUEST_URI做一些事情,但是从我的脚本中我可以看到$_SERVER['REQUEST_URI'] = '/images/file_does_not_exist.jpg'
,这表明它应该可以正常工作。
答案 0 :(得分:2)
这是因为RewriteRule在ErrorDocument之前运行。如果您使用ErrorDocument处理404 uris,则ErrorDocument不会在此处工作,因为您的规则会将所有内容重写为index.php。要解决此问题,您需要使用mod重写来处理404请求。在现有规则之前加上以下内容:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /404.php [L]
答案 1 :(得分:1)
这不是,因为我首先想到的是RewriteCond被跳过了。实际上,ErrorDocument是一个内部重定向,它匹配我的'catch all'规则。
我的一些困惑是我的本地实例没有ErrorDocument指令,因此正在使用default 'hardcoded' message - 因此没有重定向。
其次我现在意识到REQUEST_URI
不会被内部重写改变,即使是ErrorDocument也是如此。我不确定这与其他网络服务器有何不同,但将来要小心。
我现在在.htaccess中明确设置ErrorDocument 404
以避免dev和live环境之间的差异,否则我的配置会保持不变,因为我意识到它正在按预期工作。