A simple, 'no additional software' method of blocking adverts on the web is to alter the local 'hosts file', in order to redirect requests for the banner ads from the real server either to the local machine, or into oblivion, preventing the banner from being downloaded, and therefore displayed.

The hosts file is (for most operating systems) a text file, each line of which contains an ip address, followed by the hostnames it should override. Hosts files are normally used in small networks that are too small to justify a centralised DNS, to map hostnames to devices that don't participate in a peer-to-peer name resolution system. In commonly-used operating systems, the hosts file is found in the following places:

  • Windows 2000/XP: %windir%/system32/drivers/etc/hosts
  • Windows 95/98/ME: %windir%/hosts
  • Linux: /etc/hosts

This is not without its drawbacks. Quite a few web browsers (Netscape navigator especially) won't render the page until they have downloaded all the images and scripts the page needs to display. If one of these images or scripts is located on a server blocked using the hosts file, it will not render the page until it has timed out trying to contact all the blocked servers, often several times.

This problem can be circumvented, either by redirecting the requests to a nearby webserver, or by running a webserver on the local machine, and redirecting requests to it. The webserver on the local machine need not be particularly powerful (Personal Webserver will do; Apache is probably a bit overkill), or even a proper webserver (look elswhere in this node for a perl script that pretends to be a webserver).

Though it can be made to work acceptably, this method is nothing better than a hack. It is less useful on modern, multi-user operating systems, as the changes to the hosts file affect all users, and often cannot be effected without being root/administrator. In addition, there is no way to block adverts on the same host as wanted content (for example, there is no way to block http://www.example.com/ads/banner.gif without also blocking http://www.example.com/articles/interesting_article.html).

A much better solution is to use an advert-blocking proxy - privoxy is Free, and available for both windows and unix. As it matches URLs according to a list of regular expressions, it can block with much greater granularity than simply banning hosts. It can block patterns of URLs, catching all of a large ad company's servers in one line that does not need to be updated as the company moves its servers about. It serves replacement images (either transparent 'web bugs' to remove the banner entirely, or an unobtrusive checkerboard pattern, so that it is obvious that the page has been interfered with) for banners, so there are none of the 'time out' issues associated with host-based blocking. Privoxy can even alter the text of web-pages in transit to remove the place where the blocked advert would have been. Blocking and web-page altering use Perl's regular expression library, allowing for very powerful and customisable ad blocking and web-page alteration.

The hosts file does have its uses, however - several 'spyware' programs (creative labs media manager, gozilla, realplayer etc.) will attempt to 'phone home', sending personal information back to the authors, and providing no way of disabling this behaviour. In this case, adding a bogus entry for the 'phone home server to the hosts file will prevent the application from phoning home. In my opinion, the privacy benefits greatly outweigh the inconvenience of the program popping up a 'cannot connect to server' warning every so often.