I have a problem with understanding mod_rewrite behavior. I'll illustrate this with an example. I have 3 files in my root directory: .htaccess, index.php and test.php. The content of files:
.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);
?>
So when I go to my site's root folder with the browser, two strings are inserted in database - 'test_string' and 'test_string'. If I go to /test.php
, also two strings will be inserted - one from index.php script - 'test_string' and one from test.php
string - 'another_test_string'. If I remove rewrite rules from .htacess, only one string will be inserted for both pages. I cannot understand such behavior - why all scripts are executed twice? And especially I don't understand why this happens with test.php
since I wrote RewriteCond %{REQUEST_FILENAME} !-f
, so no rewrites should be done.
Thank you in advance.