【问题标题】:Allow access to specific file but exclude files of the same name in subdirectories in .htaccess允许访问特定文件,但排除 .htaccess 中子目录中的同名文件
【发布时间】:2019-04-12 15:47:57
【问题描述】:

我的目录结构如下:

  • folder
    • example
      • index.php
    • .htaccess
    • index.php
    • other.php

我想拒绝访问 /folder 中的所有内容,/folder/index.php 除外。我可以拒绝访问所有文件,然后允许访问index.php,但这也允许访问位于.htaccess 子目录中的任何index.php

Order allow,deny
Deny from all
<FilesMatch ^index\.php$>
    Allow from all
</FilesMatch>

如何修改.htaccess 以仅允许访问与.htaccess 位于同一目录中的index.php?请注意,所有文件夹和/或文件名都可能更改,index.php.htaccess 除外。

【问题讨论】:

    标签: .htaccess


    【解决方案1】:

    您可以使用此mod-rewrite 规则来拒绝对/folder 中除/index.php 之外的所有文件的访问。

     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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-26
      • 1970-01-01
      • 2016-03-23
      • 1970-01-01
      • 2013-07-31
      • 2016-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多