通配符的.htaccess并拒绝在子域的一些地址通配符、地址、htaccess

2023-09-02 09:58:49 作者:放肆的依赖只为沵存在

我希望所有的文件或目录存在下 up.example.com 子域名被拒绝,并显示404错误。

I want all file or directory existed under up.example.com subdomain rejected and show 404 error.

此外,我希望所有其他请求指向的index.php

Also I want all another request point to index.php.

在我写这篇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]

当我测试此$ C $下 https://m.xsw88.com/allimgs/daicuo/20230902/199.png.jpg

When I test this code for https://m.xsw88.com/allimgs/daicuo/20230902/199.png.jpg

如果 test.jpg放在文件存在根服务器上,让我这个错误:

If test.jpg file existed on root server, show me this error:

Not Found
The requested URL /test.jpg was not found on this server.
Apache Server at up.example.com Port 80

这没关系! 但是,当 test.jpg放在不存在了,给我这个错误:

It's ok! But when test.jpg not existed, give me this error:

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 存在于根)

为什么呢?我该如何解决?

Why? and how can I fix it?

推荐答案

其实,问题是,你写的一切的index.php ,并且成为了一个有效的文件请求。当的mod_rewrite 运行下一次第一条规则将其发送给404。

Actually the problem is that you write everything to index.php and that becomes a valid file in the request. When mod_rewrite runs next time first rule sends it to 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]