Strange behavior in apache mod_rewrite

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.

+4
source share
1 answer

The php code you have will be inserted into the database after processing the code snippet.

If you execute it more than once, you will see more attachments in the database.

However, rewriting rules are executed only once.

So, what you're experiencing is probably not completely related to your rewriting rules. If you do not trust me, enable logging for dubbing ( see Apace docs ) and follow the trail.

You are probably sitting too long in front of the computer, so just have a cup of tea, relax and find this mistake.

+2
source

All Articles