I'm no hero with .htaccess, but I'm experiencing a problem that I think might be caused by it. The first symptoms: simple changes to my database (like user IP tracking) got done multiple times per request, while the output by the site looks just fine.
After hours of tracing the problem I finally came to the conclusion that this wasn't caused by the database itself (100% sure) and also not by the extremely simple piece of PHP code I wrote (also 100% sure). Still, the problem occurred on different servers. My only conclusion is that it has to do with my .htaccess files. To me it seems like each request is handled a couple of times before the run that actually causes output.
In my public_html folder I placed the following .htaccess file, which is supposed to redirect every request to a "public" folder:
<IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^$ public/ [L] RewriteRule (.*) public/$1 [L] </IfModule>
In that "public" folder I have another .htaccess file:
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?requestedurl=$1 [PT,L] </IfModule>
This file is meant to handle every request using an index.php file. Note: If I rename the index.php file and access it directly, the problem does not occur.
I simply don't get this behaviour and I was hoping anyone on this planet does. Thanks in advance, Hans
You may read this previous answer: RewriteRule causes page to reload twice
The. htaccess can loop, even infinite loops, but at the end there's only one request for the server. So this is not your problem. Check the link for things that can make the request being sent several time by the browser (that happen more than we usually think).
Theoretically no, all the rewrite rules get processed before any file is ever executed. The only thing that could cause multiple recalls to the database is faulty PHP code, which could be calling a line multiple times or possibly redirecting the page after the call, and then the htaccess is redirecting it back to the same page. An htaccess file alone could never do this.
I would not rule out the possibility, because programs and such can do some very random things at times, but you need to debug your PHP code more in order to eliminate it as the source. The easy way is to simply go through and every time PHP does something major, add a print line to indicate that it is executing some certain function, especially for MySQL commands. This is a quick way to see what all it is doing for that page load. You can be surprised what you find sometimes.
Also, look through your Apache access log. It will print a new line for each GET request sent by the user. If the page is never redirected, it's not possible that the script was run multiple times by the user (as pointed out above, no file is executed until the final path is determined). If you're seeing multiple GET requests from the same IP at the same time, something is redirecting or reloading the page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With