【问题标题】:Deny direct access to all .php files except index.php拒绝直接访问除 index.php 之外的所有 .php 文件
【发布时间】:2010-11-23 07:46:53
【问题描述】:

我想拒绝对所有.php 文件的直接访问,除了一个:index.php

对其他.php文件的唯一访问应该是通过php include

如果可能,我希望所有文件都在同一个文件夹中。

更新:

一般规则会很好,所以我不需要浏览所有文件。风险是我忘记了文件或行。

更新 2:

index.php 位于文件夹 www.myadress.com/myfolder/index.php

我想拒绝访问myfolder 中的所有.php 文件以及该文件夹的子文件夹。

【问题讨论】:

    标签: php .htaccess


    【解决方案1】:

    您可以尝试在index.php 中定义一个常量并添加类似

    if (!defined("YOUR_CONSTANT")) die('No direct access');
    

    到其他文件的开头。

    或者,您可以使用 mod_rewrite 并将请求重定向到 index.php,像这样编辑 .htaccess

    RewriteEngine on
    RewriteCond $1 !^(index\.php)
    RewriteRule ^(.*)$ /index.php/$1 [L,R=301]
    

    然后你应该能够分析 index.php 中所有传入的请求并采取相应的操作。

    例如,如果您想省略所有 *.jpg、*.gif、*.css 和 *.png 文件,那么您应该像这样编辑第二行:

    RewriteCond $1 !^(index\.php|*\.jpg|*\.gif|*\.css|*\.png)
    

    【讨论】:

    • 你知道吗?我不知道。
    • index.php 在文件夹中是否重要。
    • 是的,你应该在 RewriteRule 中写入完整的绝对路径,比如 RewriteRule ^(.*)$ /somedir/anotherdir/index.php/$1 [L]
    • .htaccess: RewriteRule: unknown flag 'L][R'
    • 已编辑:标志应以逗号分隔
    【解决方案2】:

    您确定要这样做吗?甚至 css 和 js 文件和图像和...?

    好的,首先检查mod_access是否安装到apache,然后将以下内容添加到您的.htaccess中:

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

    第一个指令禁止访问除本地主机之外的任何文件,因为Order Deny,Allow,稍后应用允许,第二个指令仅影响 index.php。

    警告:订单行中逗号后没有空格。

    要允许访问匹配 *.css 或 *.js 的文件,请使用此指令:

    <FilesMatch ".*\.(css|js)$">
        Order Allow,Deny
        Allow from all
    </FilesMatch>
    

    但是,您不能在 .htaccess 文件中使用 &lt;Location&gt;&lt;Directory&gt; 的指令。

    您的选择是在第一个允许、拒绝组周围使用&lt;FilesMatch ".*\.php$"&gt;,然后明确允许访问 index.php。

    Apache 2.4 更新: 这个答案对于 Apache 2.2 是正确的。在 Apache 2.4 中,access control paradigm 发生了变化,正确的语法是使用 Require all denied

    【讨论】:

    • 嗯...不,仅适用于 .php 文件
    • 好的,我对 .htaccess 完全是新手 我试图复制粘贴你的代码,它可以拒绝除 index.php 之外的所有文件,但不允许非 .php 文件。这是我的 .htaccess 文件: Order Deny,Allow Deny from all Allow from 127.0.0.1 Order Allow,Deny Allow from all Order Allow, Deny Allow from all
    • 对我不起作用(但我没有对你投反对票)。我收到“服务器配置错误”消息。也许服务器真的配置错误。这有效:stackoverflow.com/a/1607587/539300
    【解决方案3】:

    如何将除 index.php 之外的所有 .php 文件保留在 Web 根目录之上?无需任何重写规则或程序性杂物。

    将包含文件夹添加到您的包含路径将有助于保持简单,无需使用绝对路径等。

    【讨论】:

    • 好的,我将文件移动到另一个文件夹。感谢所有 .htaccess 帮助,但这对我来说太复杂了。
    • 或者可能不是,我的文档之间的链接引用太多,所以我等待这个解决方案。
    • 链接引用是什么意思?您是否将移动文件的文件夹添加到包含路径中?
    • 链接引用 = include('file.php')include('myfolder/file.php')
    • 如果您在包含路径 (ini_set('include_path', ini_get('include_pat'). ':/path/to/files');) 中包含将文件移动到的文件夹,那么您可以执行相同的操作而不会造成任何破坏。
    【解决方案4】:

    这个问题的一个间接答案是将所有代码编写为类,除了 index.php 文件,它们是唯一的入口点。包含类的 PHP 文件不会导致任何事情发生,即使它们是直接通过 Apache 调用的。

    直接的答案是在 .htaccess 中包含以下内容:

    <FilesMatch "\.php$">
        Order Allow,Deny
        Deny from all
    </FilesMatch>
    <FilesMatch "index[0-9]?\.php$">
        Order Allow,Deny
        Allow from all
    </FilesMatch>
    

    这将允许访问任何文件,如 index.php、index2.php 等,但会拒绝对其他 .php 文件进行任何类型的访问。它不会影响其他文件类型。

    【讨论】:

    • 如果你使用 Joomla,Joomla 网站通常只有 index.php 和 index2.php。请注意 index2.php 自 Joomla 1.6 起已弃用。
    • 好东西,但是当您的应用程序中有其他 index.php(这在某些框架中的视图中很常见)时,这会被破坏。
    【解决方案5】:

    一个简单的解决方案是将所有非 index.php 文件重命名为 .inc,然后拒绝访问 *.inc 文件。我在很多项目中都使用了它,而且效果很好。

    【讨论】:

      【解决方案6】:

      实际上,我来到这里的问题与该主题的创建者相同,但所提供的解决方案都不是我问题的完整答案。 当您只需配置一次代码时,为什么要向服务器上的所有文件添加代码? 最接近的是 Residuum 的那个,但他仍然排除了所有文件,而我只想排除未命名为 index.php 的 php 文件。

      所以我想出了一个 .htaccess 包含这个:

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

      (记住,htaccess 文件是递归工作的,所以它完全符合问题的先决条件。)

      我们开始吧。用户可以访问的唯一 php 文件是名为 index.php 的文件。 但是你仍然可以访问每个图像、css 样式表、js 脚本等。

      【讨论】:

        【解决方案7】:

        URL 重写可用于将 URL 映射到 .php 文件。以下规则可以识别 .php 请求是直接发出的还是被重写的。它禁止第一种情况下的请求:

        RewriteEngine On
        RewriteCond %{THE_REQUEST} ^.+?\ [^?]+\.php[?\ ]
        RewriteRule \.php$ - [F]
        RewriteRule test index.php
        

        这些规则将禁止所有以.php 结尾的请求。但是,仍然允许使用诸如 /(触发 index.php)、/test(重写为 index.php)和 /test?f=index.php(在查询字符串中包含 index.php)等 URL。

        THE_REQUEST 包含浏览器发送到服务器的完整 HTTP 请求行(例如,GET /index.php?foo=bar HTTP/1.1

        【讨论】:

        • 这是我一直在寻找的解决方案。我想拒绝访问除我的重写规则之外的所有 php。谢谢!
        • 你的答案离我要找的很近,如果你还在这里可能你可以看看stackoverflow.com/questions/57260003/…
        【解决方案8】:

        我不是在周围传递一个变量,而是在您不想直接访问的任何页面的顶部自包含,这仍应与 .htaccess 规则配对,但我如果 htaccess 出现故障,知道有故障保险装置会更安全。

        <?php
        // Security check: Deny direct file access; must be loaded through index
        if (count(get_included_files()) == 1) {
            header("Location: index.php"); // Send to index
            die("403"); // Must include to stop PHP from continuing
        }
        ?>
        

        【讨论】:

        • 这对于小型应用程序来说很好,但是当您有大量控制器、模型和模板等时,这将变得完全无法管理。
        【解决方案9】:

        只允许 2 个 ip ,所有其他将阻止

        Order Deny,Allow
        Deny from all
        Allow from 173.11.227.73 108.222.245.179
        

        【讨论】:

          【解决方案10】:

          将所有文件放在一个文件夹中。在该文件夹中放置一个 .htaccess 文件,并为其赋予值deny all。然后在放置在文件夹之外的 index.php 中,让它根据用户输入或事件回显正确的页面

          【讨论】:

            【解决方案11】:
            <Files ~ "^.*\.([Pp][Hh][Pp])">
             Order allow,deny
             Deny from all
             Satisfy All
            </Files>
            

            【讨论】:

              【解决方案12】:

              在 Apache 2.4 中,访问控制的语法发生了变化。如果使用如下指令:

              Order deny,allow
              Deny from all
              

              不起作用,您可能使用的是 Apache 2.4。

              Apache 2.4 docs are here

              您需要通过以下方式之一使用Require all denied

              # you can do it this way (with "not match")
              
              <FilesMatch ^((?!index\.php).)*$>
                Require all denied
              </FilesMatch>
              
              # or 
              
              Require all denied
              
              <Files index.php>
                Require all granted
              </Files>
              

              【讨论】:

              猜你喜欢
              • 2012-10-26
              • 2011-12-15
              • 2011-03-25
              • 1970-01-01
              • 2016-08-20
              • 2015-09-13
              • 2020-01-13
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多