我具有以下目录结构:
folder
example
index.php
.htaccess
index.php
other.php
我想拒绝访问/folder
中除/folder/index.php
之外的所有内容。我可以拒绝访问所有文件,然后允许访问index.php
,但这也允许访问index.php
子目录中的任何.htaccess
。
Order allow,deny
Deny from all
<FilesMatch ^index\.php$>
Allow from all
</FilesMatch>
如何修改.htaccess
以仅允许访问index.php
所在目录中的.htaccess
?请注意,除了index.php
和.htaccess
之外,所有文件夹和/或文件名都可以更改。
答案 0 :(得分:1)
您可以使用此mod-rewrite
规则拒绝访问/index.php
中除/folder
之外的所有文件。
RewriteEngine on
#check if the file exists
RewriteCond %{REQUEST_FILENAME} -f
#check if the file is being requestsed from /folder
RewriteCond %{REQUEST_URI} ^/folder/
#deny access to all existant files except index.php
RewriteRule !folder/index\.php - [L,R=403]
如果浏览器访问/index.php
以外的所有文件,这将返回403禁止错误。
上面的规则仅适用于/folder
中的文件。如果要拒绝访问所有文件(包括文件和子文件夹),只需删除第一个条件RewriteCond %{REQUEST_FILENAME} -f
即可。