【问题标题】:Deny all files, but index/default page with htaccess拒绝所有文件,但使用 htaccess 索引/默认页面
【发布时间】:2012-08-20 07:21:06
【问题描述】:

我想拒绝目录中的所有文件,但 index.php(作为默认页面)。

这个解决方案几乎可以工作:

Deny from all
<Files index.php>
    Order Allow,Deny
    Allow from all
</Files>

唯一的问题:'upload/index.php' 现在可以访问了,但 '/upload/' 不是。如何使用 htaccess 允许默认页面?

【问题讨论】:

    标签: apache .htaccess


    【解决方案1】:

    您可以尝试使用 mod_rewrite,将您拥有的替换为:

    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^/(index.php)?$
    RewriteRul ^ - [L,F]
    

    【讨论】:

      【解决方案2】:

      您的问题是,正如您可能已经发现的那样,您拒绝所有的东西,然后允许 URI 'index.php',但不允许 URI '/' - 即使 '/' 得到重定向到幕后的 index.php,它仍然是一个不同的 URI,因此它也应该被允许。

      最简单的方法是使用FilesMatch 指令,如下所示:

      order allow,deny
      <FilesMatch "^(index\.php)?$">
          allow from all
      </FilesMatch>
      

      正则表达式^(index\.php)?$ 表示“index.php 或无”。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-30
        • 2015-09-13
        • 1970-01-01
        • 2020-02-19
        • 1970-01-01
        • 2013-02-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多