I have a problem understanding mod_rewrite behavior. I will illustrate this with an example. I have 3 files in my root directory: .htaccess , index.php and test.php . File Contents:
.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); ?>
Therefore, when I go to the root folder of my site with a browser, two lines are inserted into the database - "test_string" and "test_string". If I go to /test.php
, two lines will be added: one of the index.PHP script is 'test_string' and one of the test.php
string is 'another_test_string'. If I remove the rewrite rules with .htacess, only one line will be inserted for both pages. I do not understand this behavior - why are all the scripts executed twice? And especially I donโt understand why this happens with test.php
with I wrote RewriteCond %{REQUEST_FILENAME} !-f
, so rewriting should not be done.
Thanks in advance.
source share