【问题标题】:Apache Conf - FilesMatch allow only in current folder, not subfolderApache Conf - FilesMatch 只允许在当前文件夹中,而不是子文件夹中
【发布时间】:2016-07-20 21:46:38
【问题描述】:

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

当前设置:

<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 文件夹中的文件也无法访问。

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

亲切的问候,

多米尼克

【问题讨论】:

  • 您的设置有问题:您无法用&lt;/FilesMatch&gt; 标签关闭打开的&lt;Files&gt; 标签。查看文档:httpd.apache.org/docs/current/mod/core.html#filesmatch 这当然应该在您的 http 服务器错误日志文件中指出。更改设置时需要监控该文件。
  • 你好 arkascha,谢谢你的提示。不幸的是,这个错误在真正的配置文件中不存在。在这里写问题时出现了这个错误。即使使用正确的打开和关闭标签,也会出现此问题
  • IIRC,那么您可以将 &lt;FilesMatch&gt; 指令放在 &lt;Directory&gt; 标记内。您可以使用通配符为子文件夹定义另一个目录标签(例如&lt;Directory /some/path/*&gt;)并再次使用拒绝访问&lt;FilesMatch&gt; 指令。可能值得一试。

标签: apache .htaccess httpd.conf


【解决方案1】:

感谢阿卡沙,

DirectoryMatch 方法是错误的想法。

解决方案很简单 - 正如 arkascha 推荐的那样:

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

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

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

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

非常感谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-08
    • 2018-05-04
    • 2015-01-14
    • 2014-04-15
    • 1970-01-01
    相关资源
    最近更新 更多