我对理解 mod_rewrite 行为有疑问。 我将用一个例子来说明这一点。 我的根目录中有 3 个文件:.htaccess、index.php 和 test.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/