繁体   English   中英

通配符.htaccess并拒绝子域上的某些地址

[英]wildcard .htaccess and reject some address on subdomain

我希望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 (root上存在index.php

为什么? 以及如何解决?

试试这个

RewriteEngine On

RewriteCond %{HTTP_HOST} ^up\.(.+)$ [NC]  # remove if it is not related to subdomain
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L,QSA]

RewriteCond %{HTTP_HOST} ^up\.(.+)$ [NC]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteRule ^(.*)$ - [R=404,L,NS]

实际上,问题是您将所有内容都写入了index.php并且在请求中变成了有效文件。 下次运行mod_rewrite时,第一个规则将其发送到404。

要克服此错误,请遵循以下规则:

RewriteEngine On

# if it is valid file or directory except for index.php
# then send 404 to browser
RewriteCond %{HTTP_HOST} ^up\. [NC]
RewriteCond %{THE_REQUEST} \s/+index\.php[?\s] [NC]
RewriteRule ^index\.php$ - [R=404,L,NC]

RewriteCond %{HTTP_HOST} ^up\. [NC]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule !^index\.php$ - [R=404,L,NC]

# send all non-file/directories to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM