【发布时间】:2014-10-18 01:17:52
【问题描述】:
我希望 up.example.com 子域下存在的所有文件或目录被拒绝并显示 404 错误。
我还希望所有其他请求都指向index.php。
我写了这段代码:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^up\.(.+)$ [NC]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ - [R=404,L,NS]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L,QSA]
当我为http://up.example.com/test.jpg测试这段代码时
如果根服务器上存在test.jpg 文件,请显示此错误:
Not Found
The requested URL /test.jpg was not found on this server.
Apache Server at up.example.com Port 80
没关系!
但是当test.jpg不存在时,给我这个错误:
Not Found
The requested URL /index.php was not found on this server.
Apache Server at up.example.com Port 80
改为打开index.php(index.php 存在于根目录中)
为什么?我该如何解决?
【问题讨论】:
-
第一组规则不行。它结合了前两个 RewriteCond (默认)和 OR 到第三个之间的 AND。您还必须添加排除 index.php。
标签: regex apache .htaccess mod-rewrite redirect