使用.htaccess在除根目录外的任何位置阻止index.php


Block index.php everywhere except in the root directory with .htaccess

我有一个这样的文件夹结构:

subdir
|_ subsubdir
  |_ index.php
  |_ other_stuff.php
  |_ images
    |_ pic.jpg
.htaccess
config.ini
index.php

.htacces文件的内容:

Options +FollowSymLinks -MultiViews -Indexes
Order Deny,Allow
<FilesMatch "('.ini$)|('.php$)|(^'.)">
    Deny from all
</FilesMatch>
<Files index.php>
    Allow from all
</Files>

我的目标是防止用户查看subdir/suubbdir/index.php(以及所有其他*.php文件,无论在哪里,但这已经有效了),但允许用户在根目录中查看index.php。

目前,用户显然仍然可以看到subdir/suubbdir/index.php,因为所有名为"index.php"的文件都是允许的,但我不知道如何允许只允许根目录中的文件,并拒绝所有其他文件。我尝试过不同的事情,但最终要么完全否认,要么全部允许。

我觉得这应该是一项非常容易的任务,但我就是想不通。

我尝试过的东西:

<FilesMatch "'.'/index'.php">
    Allow from all
</FilesMatch>
---
<FilesMatch "^index'.php$">
    Allow from all
</FilesMatch>
---
<Files ./index.php>
    Allow from all
</Files>
---
<Files /index.php>
    Allow from all
</Files>

注释掉两个FilesMatch块:

并使用此代码:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index'.php$ [NC]
RewriteRule ^'.|'.(ini|php)$ - [F,NC]