Is my webserver allowing pre-fetching?
I have an image based website that hosts multiple galleries. When a user clicks one of the images the views
property for the particular image is incremented. However, I noticed when using Firefox (14.0.0.1) it increments the views twice, whereas not-so-complicated browsers such as K-meleon increment the property once - as it should.
Someone then pointed out on stackoverflow that this is probably due to pre-fetching in modern browsers causing the PHP script that fetches the image from the database and increments the views to be executed twice. So, checking my apache access.log I noticed that Firefox does indeed request each image twice:
Firefox/14.0.1" ::1 - - [03/Aug/2012:16:41:37 +0100] "GET /imgsite/getImg.php?id=23 HTTP/1.1" 200 57362 "http://localhost/imgsite/index.php" "Mozilla/5.0 (Windows NT 6.1; rv:14.0) Gecko/20100101 Firefox/14.0.1" ::1 - - [03/Aug/2012:16:41:37 +0100] "GET /imgsite/getImg.php?id=23 HTTP/1.1" 200 57362 "-" "Mozilla/5.0 (Windows NT 6.1; rv:14.0) Gecko/20100101 Firefox/14.0.1"
I then read up on .htaccess and tried several methods of blocking the pre-fetch headers that Firefox (or chrome) sends, but shouldn't these headers show up in the above snippet? Nothing has worked so far. Here is my .htaccess file that is placed in the root HTML path of my website:
RewriteEngine On
RewriteCond %{HTTP:X-moz} prefetch
RewriteRule . . [F,L]
I've tried other and more extensive variants of the above, still to no avail. My apache setup allows .htaccess as the mod_rewrite.so line is enabled.
Everything I've seen plus rewrite not affecting anything is leading me to believe that this is not actually a pre-fetching issue.
If someone could shed some light on this issue I'd be very grateful. Thanks.
I found this question interesting because I've had some trouble with this as well.
According to this site Prefetching Hints there is (from Firefox anyway) a specific header in each request (X-moz: prefetch). This site shows some ways to identify those headers and log them, as well as some ways to try to force prefetching to behave a little bit more friendly.
At least if you can get the information logged you can determine whether or not it is indeed prefetching causing your issue.
I haven't tried anything from this webpage yet, so no telling whether it works or not. I did bookmark it for some future testing. It's very frustrating sometimes the way modern full featured browsers will prefetch significant portions of a site and cause little issues such as incrementing the number of views. It is a great thing when you are on the operating end of the web browser, I'll admit. Something nice about click and an immediate display of the entire page, graphics and all.
Hopefully this information can point you down the right path in at least diagnosing your problem, if not even solving it! Good luck!
Note that in your logs the second request does not have a referrer field. So these requests are not exactly identical. I suspect pre-fetching may be the issue here.
Also check the HTTP headers on the file? If you are sending a no-cache header due to the PHP processing, this could be part of the issue. Note that both responses are a 200 OK instead of a 304 response if your cache was working.
Some ideas:
1) Run simple test to confirm htaccess rewrite rules are being processes. Could be they are being ignored.
2) If you have not already, try:
RewriteEngine On
RewriteCond %{HTTP_X_MOZ} ^prefetch$
RewriteRule ^.*$ - [F]
3) User Curl
curl --header "X-moz: prefetch" www.google.com
To do testing from the command line.
For good discussion on log impacts of prefecth and and other items on logs see:
http://www.adrianworlddesign.com/Knowledge-Base/Zend-Framework/Helpful-Tips/Browsers-refresh-reload-your-pages