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

标签 php .htaccess

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

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

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

更新:

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

更新 2:

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

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

最佳答案

您确定要这样做吗?甚至 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>

您不能对 <Location> 使用指令或 <Directory>不过,在 .htaccess 文件中。

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

Apache 2.4 更新: 这个答案对于 Apache 2.2 是正确的。在 Apache 2.4 中 access control paradigm已更改,正确的语法是使用 Require all denied .

关于php - 拒绝直接访问除 index.php 之外的所有 .php 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1340001/

相关文章:

.htaccess - 将页面重定向到不同域下的同一页面

apache - 如果找不到页面,.htaccess 301 重定向到主页

php - Apache 服务器日志错误。 AH00126 : Invalid URI in request GET, 等

.htaccess - mod_rewrite 在大写目录上失败

php - 从 .htaccess 中的密码保护中排除一个文件

javascript - 提交表单,无需刷新并将 mysql 返回的行追加到表中

.htaccess - htaccess 重写会破坏相对路径

php - 如何将一个表中的ID替换为另一表中的相应值?

PHP/SQL - 使用 Select 元素显示/链接到数据库中的行

PHP/HTML/CSS : IE acting weird when using require_once()