拒绝直接访问除index.php之外的所有.php文件

Joh*_*han 75 php .htaccess

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

唯一访问其他.php文件应该通过PHP include.

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

更新:

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

更新2:

index.php是一个文件夹中www.myadress.com/myfolder/index.php

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

Res*_*uum 104

你确定,你想这样做吗?甚至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>
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

您不能使用的指令<Location><Directory>内部.htaccess文件,虽然.

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

Apache 2.4的更新: 这个答案适用于Apache 2.2.在Apache 2.4中,访问控制范例已经改变,并且使用了正确的语法Require all denied.

  • 嗯......不,仅适用于.php文件 (2认同)
  • 对我不起作用(但我没有向你投票).我收到了"服务器配置错误"的消息.也许服务器确实配置错误.这有效:http://stackoverflow.com/a/1607587/539300 (2认同)

Jer*_*ska 81

实际上,我带着与该主题的创建者相同的问题来到这里,但所给出的解决方案都不是我问题的完整答案.为什么只需配置一次代码就可以为服务器上的所有文件添加代码?最接近的是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>
Run Code Online (Sandbox Code Playgroud)

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

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


use*_*631 34

这个问题的一个倾斜的答案是将所有代码写为类,除了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>
Run Code Online (Sandbox Code Playgroud)

这将允许类似的index.php,index2.php等进行访问的任何文件,但会拒绝任何形式的其他.php文件的访问权限.它不会影响其他文件类型.


n13*_*313 17

你可以尝试定义一个常量index.php并添加类似的东西

if (!defined("YOUR_CONSTANT")) die('No direct access');
Run Code Online (Sandbox Code Playgroud)

到其他文件的开头.

或者,您可以使用mod_rewrite和重定向请求到index.php,编辑.htaccess如下:

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

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

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

RewriteCond $1 !^(index\.php|*\.jpg|*\.gif|*\.css|*\.png)
Run Code Online (Sandbox Code Playgroud)


nik*_*org 11

除了在web根目录之上的index.php之外,保留所有.php文件怎么样?无需任何重写规则或程序化的kludges.

将includes-folder添加到include路径将有助于简化操作,无需使用绝对路径等.


Sal*_*n A 6

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

RewriteEngine On
RewriteCond %{THE_REQUEST} ^.+?\ [^?]+\.php[?\ ]
RewriteRule \.php$ - [F]
RewriteRule test index.php
Run Code Online (Sandbox Code Playgroud)

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

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


Jos*_*iah 5

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

Order deny,allow
Deny from all
Run Code Online (Sandbox Code Playgroud)

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

Apache 2.4 文档位于此处

您需要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>
Run Code Online (Sandbox Code Playgroud)