Apache Conf - FilesMatch 仅允许在当前文件夹中,而不是子文件夹中

问题描述 投票:0回答:1

是否可以允许通过 访问特定文件,以便此规则不适用于子文件夹中的同名文件?

当前设置:

<FilesMatch "\.php$">
    Deny from all
</FilesMatch>

<Directory /var/www/>
    <Files index.php>
        Allow from all
    </Files>
</Direcotry>

<Directory /var/www/admin/>
    <Files index.php>
        Allow from all
    </Files>
</Direcotry>

我们假设以下情况:

  1. 我们在 web-root 中有一个 index.php,在子文件夹 admin 中有一个,在其他各个子文件夹中,也有一些 index.php 文件。
  2. 如果没有明确允许,则不应访问 *.php - 在本例中仅 /index.php 和 /admin/index.php

发生了什么:

当允许访问 webroots index.php 时,所有子文件夹中的所有 index.php 文件都将变得可访问。

尝试过什么:

使用 DirectoryMatch 禁止访问子文件夹中的 index.php。

<FilesMatch ".+\.php$">
    Deny from all
</FilesMatch>

<Directory /var/www/>
    <Files index.php>
        Allow from all
    </Files>
</Direcotry>

<DirectoryMatch "/var/www/(.+)/">
    <Files index.php>
        Deny from all
    </Files>
</DirectoryMatch>

<Directory /var/www/admin/>
    <Files index.php>
        Allow from all
    </Files>
</Direcotry>

现在,禁止访问所有子文件夹中的所有 index.php 文件 - 但仍应允许访问 /admin/ 中的 index.php。

还尝试使用文件的绝对路径

<FilesMatch ".+\.php$">
    Deny from all
</FilesMatch>
<Files /var/www/index.php>
    Allow from all
</Files>
<Files /var/www/admin/index.php>
    Allow from all
</Files>

这也行不通。 没有 index.php 文件可访问 - 即使是 webroot 和 admin-folder 中的文件也无法访问。

我相信有更好的方法来解决这样的问题?

亲切的问候,

多米尼克

apache .htaccess httpd.conf
1个回答
0
投票

感谢阿卡查,

DirectoryMatch 方法是错误的想法。

这个问题的解决方案很简单 - 正如 arkascha 所推荐的:

<FilesMatch "\.php$">
    Deny from all
</FilesMatch>

<Directory /var/www/>
    <Files index.php">
        Allow from all
    </Files>
</Directory>

<Directory /var/www/*/*">
    <Files index.php>
        Deny from all
    </Files>
</Directory>

<Directory /var/www/admin/>
    <Files index.php>
        Allow from all
    </Files>
</Directory>

非常感谢。

© www.soinside.com 2019 - 2024. All rights reserved.