允许访问特定文件,但在.htaccess

时间:2019-04-12 15:47:57

标签: .htaccess

我具有以下目录结构:

  • 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之外,所有文件夹和/或文件名都可以更改。

1 个答案:

答案 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即可。