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

  • 1
    Have you tried running Firefox with no extensions enabled? – Michael Hampton Aug 6 '12 at 23:08
  • 2
    That's really weird. I've certainly seen a HEAD request to check/confirm caching information, but I've never seen two actual GET requests for the same resource – Mark Henderson Aug 7 '12 at 1:10
  • Maybe you can try open the firefox webconsole to see what happened indeed. – Sam Liao Aug 7 '12 at 3:10
  • Have you confirmed your .htaccess is actually being read? If you put in another generic htaccess rewrite rule is it processed? – jeffatrackaid Aug 7 '12 at 19:22

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:



My $0.02, don't just blindly increment on a page view, that's a recipe for manipulation. Do something based on client ip address / time and that will ensure things like prefetch won't take effect.

The other part is don't run that routine if prefetch is set in the header.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.