阿帕奇mod_rewrite中的奇怪行为


Strange behavior in apache mod_rewrite

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

.htaccess

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

索引.php

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

测试.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 ,还将插入两个字符串 - 一个来自索引.php脚本 - "test_string",一个来自test.php字符串 - "another_test_string"。如果我从 .htacess 中删除重写规则,则只会为两个页面插入一个字符串。我无法理解这种行为 - 为什么所有脚本都执行两次?特别是我不明白为什么自 I wrote RewriteCond %{REQUEST_FILENAME} !-f 年以来test.php会发生这种情况,因此不应进行重写。
提前谢谢你。

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

如果多次执行它,您将在数据库中看到更多插入。

但是,重写规则也只执行一次。

因此,您所经历的可能与您的重写规则完全无关。如果您不信任我,请为重写启用日志记录(请参阅apace文档)并遵循跟踪。

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