6

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

Mr.H
  • 676
  • 5
  • 9

2 Answers2

4

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).

Community
  • 1
  • 1
regilero
  • 29,806
  • 6
  • 60
  • 99
3

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.

animuson
  • 53,861
  • 28
  • 137
  • 147
  • Dear animuson, Thanks for your quick reply. You gave me a huge clue by making me realize that a request could cause secondary requests (via the client). Non-existing files would also be redirected to index.php. For example: the HTML referred to a missing favicon.ico file. Removing that line fixed one of the iterations. I still need to find one though, which I can't find in the HTML. I'll try to look into that Apache log file for that. Thanks. Would it also be an idea to make the PHP check for the requested mime-type and call off any request not expecting HTML? Thanks again, Hans – Mr.H Dec 05 '11 at 00:23
  • Requests don't send a MIME-type that they're *expecting* to receive, but the browser can receive a hint to what it should expect (even though it's still not sent). It would be better to include all your resources such as CSS, images, and JavaScript in a single folder and exclude that folder from your rewrites. – animuson Dec 05 '11 at 00:26
  • Thanks for thinking along! I think I will try to figure out how to exclude files that should be in excising folders, instead of only excluding actually existing files. Also: I removed the last cause of the problem, so it's currently working fine, without any secondary requests, thanks to you. – Mr.H Dec 05 '11 at 00:43