是否可以允许通过
当前设置:
<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>
让我们假设如下:
- 我们在 web-root 中有一个 index.php,在子文件夹 admin 中有一个,在其他各个子文件夹中,还有一些 index.php 文件。
- 如果没有明确允许,则不应访问 *.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/