php - apache mod_rewrite 中的奇怪行为

标签 php apache .htaccess mod-rewrite

我对理解 mod_rewrite 行为有疑问。 我将用一个例子来说明这一点。 我的根目录中有 3 个文件:.htaccessindex.phptest.php。 文件内容:

.htaccess

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+) ?link=$1 [L]

index.php

<?php
$db = mysqli_connect('localhost', 'root', '', 'mydb'); 
$db->real_query ("INSERT INTO `test` (`str`) VALUES ('test_string')");
print_r($_GET);
?>

test.php

<?php
$db = mysqli_connect('localhost', 'root', '', 'mydb'); 
$db->real_query ("INSERT INTO `test` (`str`) VALUES ('another_test_string')");
print_r($_GET);
?>

因此,当我使用浏览器访问我网站的根文件夹时,两个 字符串被插入到数据库中 - 'test_string' 和 'test_string'。如果我转到 /test.php,还会插入两个字符串 - 一个来自 index.php 脚本 - 'test_string',另一个来自 test.php 字符串 - 'another_test_string '.如果我从 .htacess 中删除重写规则,则只会为两个页面插入一个字符串。我无法理解这种行为 - 为什么所有脚本都执行两次?特别是我不明白为什么 test.php 会发生这种情况,因为 我写了 RewriteCond %{REQUEST_FILENAME} !-f,所以不应该进行重写。
提前谢谢你。

最佳答案

处理该代码片段后,您拥有的 php 代码将插入到数据库中。

如果你多次执行它,你会在数据库中看到更多的插入。

然而,重写规则也只执行一次。

所以您所经历的可能与您的重写规则完全无关。如果您不信任我,请为重写启用日志记录 ( see apace docs ) 并跟踪线索。

你可能在电脑前坐得太久了,所以喝杯茶,放松一下,找到那个 bug。

关于php - apache mod_rewrite 中的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11967992/

相关文章:

php - Apache - PHP 文件返回黑色,php 文件显示中甚至没有 HTML

php - PHP 可以移动并编辑服务器上的根系统文件吗?

apache - 在 apache RewriteRule 中找不到协议(protocol)变量

php - 从查询字符串php中删除加号

php - 使用录制的语音作为 twilio 创建调用响应

php - 语言浏览器检测和 Action 重写

linux - 重定向 301/subfolder/old-page-name.html http ://domain. com/new-page-name.html 不起作用

javascript - 验证下载文件

javascript - 如何使用ajax正确将javascript变量插入mysql数据库

php - 运行一次 PHP 函数