为什么 Apache 在没有扩展名的情况下调用文件时执行 php 文件


Why does Apache execute php file on calling file without extension

今天我在服务器上发现了一个奇怪的东西。我创建了一个php文件(test.php),并在其中编写了一些php代码。然后我用

http://127.0.0.1/test

和"测试.php"执行!当没有.php时.php如何理解运行测试?我的根目录上没有 HTaccess 文件可以告诉 Apache 这样做。我想这可能会导致安全问题。我该如何预防它?

我的操作系统是ubuntu,Web服务器是Apache2。

发生这种情况是因为多视图(它在该目录的"选项"中的某个位置启用)。

请看这里:http://httpd.apache.org/docs/current/content-negotiation.html#negotiation 有关其工作原理的详细信息。

检查你的 apache 配置(/etc/apache2/sites-available/[site_name or default]),它可能包含mod_rewrite指令,例如:

RewriteEngine on
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]

如果您用 # 注释掉它们并重新启动 apache,则在不指定扩展名的情况下访问/test 应该不再有效。