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

标签 apache .htaccess httpd.conf

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

当前设置:

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

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

亲切的问候,

多米尼克

最佳答案

感谢 arkascha,

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>

非常感谢。

关于Apache Conf - FilesMatch 只允许在当前文件夹中,而不是子文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36350351/

相关文章:

apache - htaccess - 如果末尾有问号,则剪掉问号

apache ScriptAlias cgi-bin 目录

php - 如何在 php 中创建动态 URL?

.htaccess - google-apps 域验证异常的 Codeigniter .htaccess 文件配置

apache - https在httpd docker容器上不起作用

ruby - Webrick 和 Apache 在一台服务器上

java - 在 Apache Tomcat 上部署 WAR 文件 - 不工作

.htaccess - 如何强制使用 HTTPS 和 WWW

php - Apache2.4.11 虚拟主机配置不工作

http - 如何在 apache 配置中使用通配符别名作为文件名?