Blocking Mozilla / Google Prefetch

by Pete Freitag

Mozilla browsers support a feature called link prefetching, which allows a web page to tell the browser to prefetch a url if it is idle. Google has been using this technique in their search results, telling Mozilla to start loading the first result. I also noticed that MXNA 2.0 is including 3 prefetch tags.

How to you tell the browser to prefetch a url?

By using the following code:

<link rel="prefetch" href="http://url.to.prefetch/" />

How can I detect prefetching on my web site?

When mozilla does a prefetch, it sends a header X-moz: prefetch, you can then block based on that header. Google recommends sending a 404 back to block the prefetch.

How can I block prefetching?

Using mod_rewrite to send a 404:

RewriteEngine On
SetEnvIf X-moz prefetch HAS_X-moz 
RewriteCond %{ENV:HAS_X-moz} prefetch 
RewriteRule .* /prefetch-attempt [L]

This will redirect all prefetch-attempts to /prefetch-attempt as long as that file doesn't exist, the client will get a 404.

You could also block with a 503 Forbidden response:

RewriteEngine On
SetEnvIf X-moz prefetch HAS_X-moz
RewriteCond %{ENV:HAS_X-moz} prefetch 
RewriteRule .* [F,L]

In IIS using isapi rewrite:

RewriteCond X-moz: prefetch
RewriteRule .* /prefetch-attemtp [L]

Or with ColdFusion:

<cfset httpHeaders = GetHttpRequestData().headers>
<cfif StructKeyExists(httpHeaders, "X-moz") AND httpHeaders["X-moz"] IS "prefetch">
	<cfheader statuscode="404" statustext="Not Found">
	<cfabort>
</cfif>

How can I disable prefetching on mozilla browsers?

Add this line to your prefs.js file located in your Mozilla profile directory to disable prefetching in firefox and mozilla browsers.

user_pref("network.prefetch-next",false);

Some interesting things about prefetch:

Comments

Joseph Yaroch

Using a mozilla blowswer, an easier way to disable prefetching is to type "about:config" in the address bar. This brings up a page of numerous preferences. Type "prefetch" in the filter, so that only settings that contain the string, prefetch, are shown. Then doulbe click on the prefence, and it will change from true to false.

Pete Freitag

Thanks Joseph, That is a lot easier!

James Moberg

Your ColdFusion example only works with CFMX6 or 7... do you have a method that works with CF5?

Pete Freitag

James, GetHttpRequestData() should work in CF5, what isn't working?

James Moberg

It's the syntax. I used CFDump to figure it out and it should be written like this (without GetHttpRequestData().headers): <cfset httpHeaders = GetHttpRequestData()> <cfif StructKeyExists(httpHeaders.headers, "X-moz") AND httpHeaders.headers["X-moz"] IS "prefetch"> <cfheader statuscode="404" statustext="Not Found"><cfabort> </cfif> Thanks! Your article is the only one I found concerning how to block prefetching. This is going to become more of a problem now that Google's Web Accelerator has this enabled. For more info, go to: http://37signals.com/svn/archives2/google_web_accelerator_hey_not_so_fast_an_alert_for_web_app_designers.php

sophek

Where do you put to code? In the Application.cfm file? <cfset httpHeaders = GetHttpRequestData().headers> <cfif StructKeyExists(httpHeaders, "X-moz") AND httpHeaders["X-moz"] IS "prefetch"> <cfheader statuscode="404" statustext="Not Found"> <cfabort> </cfif> thanks Sophek

ikki

I tried the mod rewrite and it does not work. I also do not see the X-moz or anything regarding prefetching in the apache logs. RewriteEngine On SetEnvIf X-moz prefetch HAS_X-moz RewriteCond %{ENV:HAS_X-moz} prefetch RewriteRule .* /prefetch-attempt [L]