[英]wildcard .htaccess and reject some address on subdomain
I want all file or directory existed under up.example.com
subdomain rejected and show 404 error. 我希望
up.example.com
子域下存在的所有文件或目录都被拒绝,并显示404错误。
Also I want all another request point to index.php
. 我也希望所有其他请求指向
index.php
。
I write this code: 我写这段代码:
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]
When I test this code for http://up.example.com/test.jpg
当我为
http://up.example.com/test.jpg
测试此代码时
If test.jpg
file existed on root server, show me this error: 如果根服务器上存在
test.jpg
文件,请显示此错误:
Not Found
The requested URL /test.jpg was not found on this server.
Apache Server at up.example.com Port 80
It's ok! 没关系!
But when test.jpg
not existed, give me this error: 但是当
test.jpg
不存在时,给我这个错误:
Not Found
The requested URL /index.php was not found on this server.
Apache Server at up.example.com Port 80
Instead open index.php
( index.php
exist on root) 而是打开
index.php
(root上存在index.php
)
Why? 为什么? and how can I fix it?
以及如何解决?
Try this one 试试这个
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]
Actually the problem is that you write everything to index.php
and that becomes a valid file in the request. 实际上,问题是您将所有内容都写入了
index.php
并且在请求中变成了有效文件。 When mod_rewrite
runs next time first rule sends it to 404. 下次运行
mod_rewrite
时,第一个规则将其发送到404。
To overcome this error have your rules like this: 要克服此错误,请遵循以下规则:
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.