【问题标题】:htaccess prevent direct .php files except index.phphtaccess 阻止除 index.php 之外的直接 .php 文件
【发布时间】:2016-08-20 15:17:28
【问题描述】:

我想阻止直接访问 .php 文件。这下面 2 行工作正常

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.php - [F,L]

但是,它不会打开 index.php 文件。我想防止直接访问 .php 文件,除了 index.php 文件

【问题讨论】:

标签: php .htaccess utf-8


【解决方案1】:

添加条件以不将规则应用于特定文件:

RewriteCond $1 !^(index\.php)$

【讨论】:

    【解决方案2】:

    要防止直接访问除index.php 之外的所有.php 文件,您可以使用此规则:

    RewriteCond %{THE_REQUEST} \.php[?/] [NC]
    RewriteRule !^index\.php$ - [F,L,NC]
    

    您需要为此使用%{THE_REQUEST} 变量。 THE_REQUEST 变量代表 Apache 从您的浏览器收到的原始请求,在执行某些重写规则后它不会被覆盖。

    【讨论】:

      【解决方案3】:
      RewriteCond %{ENV:REDIRECT_STATUS} ^$
      RewriteRule !^index\.php$ - [F,L]
      

      【讨论】:

        【解决方案4】:
        Options +FollowSymlinks
        RewriteEngine On 
        RewriteCond %{REQUEST_FILENAME} !^(.+).css$ 
        RewriteCond %{REQUEST_FILENAME} !^(.+).js$ 
        RewriteCond %{REQUEST_FILENAME} !index.php$ 
        RewriteRule ^(.+)$ /deny/ [nc] 
        

        可以访问文件“CSS”和“JS”以及“index.php”。其他请求将被重定向到“拒绝”文件夹。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-01-08
          • 2010-11-23
          • 1970-01-01
          • 2015-05-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-11-03
          相关资源
          最近更新 更多