We all know that banner ads are very annoying. But did you know that a page that’s full of banner ads requires more time to download than regular pages do? Furthermore, some users are concerned that banner-ad firms are trying to pair your Web-browsing activities with information that personally identifies you. If that happens, you can bet that this data will wind up in marketing databases. If you’re running Linux, however, you can set up an effective line of defense against banner ads. You won’t see most of these ads, and your pages will download faster. Best of all, you’ll short-circuit the banner-ad firms’ data collection conduit. Once you implement my banner-defense strategy, their data streams will stop cold.
Several approaches to banner-ad blocking exist. My favorite is squid.redir, a script that Australian programmer Craig Sanders created. To implement squid.redir, you’ll need to install and run a Web server (such as Apache), the Squid proxy server (version 1.1 or higher), and the files that squid.redir needs. Chances are that Apache—and possibly Squid—are installed on your system already. If not, check your Linux distribution’s manual for information on installing and running this software. To obtain a copy of Squid, visit the Squid Web Proxy Cache. Apache’s default configuration works fine for banner-ad blocking, and you’ll need to make only one small modification to Squid’s configuration file.
How banner blocking works
Squid is a caching proxy server that stands between your browser and the Web. Used in local area networks to cut down on access to the external Web, Squid stores copies of frequently accessed Web pages so that browsers retrieve them from the cache rather than from the external network. Banner blocking comes into the picture thanks to Squid’s ability to use redirection. Redirection uses a script that tells Squid to watch out for certain URL requests (such as ad.doubleclick.com). When a browser requests this URL, the script redirects the request to a local file, such as a GIF file that contains one blank pixel. Since the URL request never makes it out of the local network, the page appears to download much faster. You’ll actually retrieve a local file, so the page appears much more quickly.
The downside of banner blocking is that you’ll need a list of banner-ad URLs to block. Squid.redir comes with its own database of banner-ad URLs. If you come across pages with banner ads that squid.redir doesn’t block, you can add the URLs of these pages to the database. Squid.redir allows you to use wildcards when you specify URLs in the database. Most of the offending banner ads include ad somewhere in their URLs. A wildcard expression that contains this word will block most of the advertising on the Web.
Installing and configuring Squid
Follow these instructions to install and configure squid.redir on your system:
- 1. In your home directory, create a directory called squid.redir. To do so, type mkdir ~/squid.redir and press [Enter].
- 2. Use your Web browser and visit Craig Sanders’ squid.redir page. Download squid-redir.tar.gz to the squid.redir directory that you just created.
- 3. Switch to the squid.redir directory. (Type cd squid.redir and press [Enter].)
- 4. Decompress and extract the files from the downloaded squid.redir tarball by typing tar -xzvf squid-redir.tar.gz and pressing [Enter].
- 5. Copy the following files to /usr/lib/squid: Makefile, gen.squid.redir, and redir. To do so, switch to superuser, and type the following:
cp Makefile /usr/lib/squid
cp gen.squid.redir /usr/lib/squid
cp redir /usr/lib/squid
- 6. Copy the following files to the top-level file directory of your Web server: dot.gif and closeme.html. For default Apache installations on Red Hat Linux, this directory is /home/httpd/html. Use the following commands:
cp dot.gif /home/httpd/html
cp closeme.html /home/httpd/html
- 7. Go to the Squid library directory. (Type cd /usr/lib/squid and press [Enter].)
- 8. In a text editor, open gen.squid.redir and locate the line that starts with $BASE_URL=. Change the default text so that it contains the location of your Web server, like so:
- 9. In the above line, //192.168.100.10 is the location of the Web server on my local area network. Be sure to substitute your server’s location.
- 10. Save gen.squid.redir and close the file.
- 11. Open /usr/lib/squid/redir. In the left-hand column, you’ll see a list of banner ad sites to block. In the right-hand column, you’ll see the URL to which these sites are redirected. By default, the URLs are redirected to Sanders’ site, where a blank banner ad is located (//www.taz.net/au/blank_ad.gif). Use your text editor’s replace function to redirect them to your Web server’s location, followed by dot.gif. Thus, on my network, the correct location is //192.168.100.10/dot.gif. When you’ve made this change, save the file.
- 12. In the text editor, open the Squid configuration file. (It’s /etc/squid/squid.conf on Red Hat systems.) Add the following line:
- 13. Save the file and exit the text editor.
- 14. Switch to /usr/lib/squid, type make, and press [Enter]. This command creates the squid.redir script. It also restarts Squid, but the script will fail to restart on Red Hat or Red Hat-like systems. If Squid doesn’t restart, type /etc/rc.d/init.d/squid restart and press [Enter].
- 15. Open your Web browser and configure it to access the Squid proxy server. In Netscape Communicator version 4, you must click Edit on the menu bar, choose Preferences, click Advanced, and click Proxies. In the Proxies area, check Manual Proxy Configuration and click View. In the Manual Proxy Configuration dialog box, type the address of your Web server in each of the Proxy boxes and type 3128 in each of the Port boxes. Click OK until you see Netscape again.
Now try to access a site that’s loaded with banner ads. If all went well, you should see absolutely nothing where the banner ads usually exist.
Adding banner ad URLs to the squid.redir database
If you encounter Web pages with banner ads that squid.redir didn’t block, note the URL and add a regular expression to the squid.redir database. That way, these pages will be blocked in the future. For example, the New York Times on the Web contains an annoying banner ad that uses GIF animations. The ad’s URL includes the text /RealMedia/ads/. To block this site, use a text editor to add the following line to /usr/lib/squid/redir:
The above line contains the URL of the Web server that runs on my network (//192.168.100.10). Substitute the correct address for your Web server. Also, you can’t use the modified redir file without hashing it, which involves running make in /usr/lib/squid. After you’ve made these adjustments, however, you can browse the Web without encountering those annoying banner ads!
Bryan Pfaffenberger, a UNIX user since 1985, is a University of Virginia professor, an author, and a passionate advocate of Linux and open source software. A Linux Journal columnist, his recent Linux-related books include Linux Clearly Explained (Morgan-Kaufmann) and Mastering Gnome (Sybex; in press). His hobbies include messing around with his home LAN and sailing the southern Chesapeake Bay. He lives in Charlottesville, VA. If you’d like to contact Bryan, send him an e-mail.
The authors and editors have taken care in preparation of the content contained herein, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for any damages. Always have a verified backup before making any changes.