第403页禁止在.htaccess中拒绝所有用户的根目录

o2g1uqev  于 2023-01-31  发布在  其他
关注(0)|答案(1)|浏览(136)

根目录中有下一个.htaccess

RewriteEngine on
RewriteRule ^$ index.php [L]
Order Deny,Allow
Deny from all
<Files index.php>
Allow from all
</Files>

并获得403页禁止www.example.com,而不是www.example.com/index.php
URL www.example.com/index.php可用。
关闭对根目录下所有文件的访问。这些文件是由脚本生成的,文件名未知。
怎么解决呢?

kq0g1dla

kq0g1dla1#

<Files index.php>
Allow from all
</Files>

请尝试以下操作:

<FilesMatch "^(index\.php)?$">
Allow from all
</FilesMatch>
    • 更新:**添加了遗漏的 * 锚点 *!

(尽管我假设您使用的是Apache 2.4,所以您应该使用相应的Require指令,而不是OrderDenyAllow。)
或者,将所有现有指令替换为以下指令:

DirectoryIndex index.php

RewriteEngine On
RewriteRule !^(index\.php)?$ - [F]

这允许访问example.com/example.com/index.php。要阻止对index.php的直接访问,请尝试以下操作:

RewriteRule ^[^/]+$ - [F]

mod_dir(即"目录索引")在mod_rewrite之后处理。

RewriteRule ^$ index.php [L]

此规则是多余的,应由DirectoryIndex处理。

    • 更新日期:**

RewriteRule!^(index.php)?$-[F]工作,但我添加RewriteRule!^(index2.php)?$-[F]为第二个文件index2.php和它不工作...我得到403错误www.example.com/index2.php...我需要访问几个文件
通过添加另一个规则,它最终会阻止两个URL。因为一个或另一个规则总是成功的。
您可以在单个规则中使用regex * alternate *。例如:

RewriteRule !^(index\.php|index2\.php)?$ - [F]

同样的正则表达式也可以用在上面的<FilesMatch>容器中。
或者,如果您有许多此类异常,则具有多个 * conditions * 可能更易于阅读。例如:

RewriteCond %{REQUEST_URI} !=index.php
RewriteCond %{REQUEST_URI} !=index2.php
RewriteCond %{REQUEST_URI} !=index3.php
RewriteRule !^$ - [F]

但是请注意,和您原来的规则一样,这也会阻止"子目录"中的URL,而不仅仅是根目录。

相关问题