Using Apache Proxy to Suppress Banner Ads

If you find Internet advertising annoying, time wasting and resource-consuming, get rid of it.
Configuring the Rewrite Engine

Now that you have the Apache Proxy configured and the browsers set up to use it, you can get to the heart of the matter, which is redirecting banner ads to a local graphic, saving yourself bandwidth, time and money.

Before you actually decide which URLs to redirect, you need to do the basic setup for mod_rewrite. Add the following lines to Apache's configuration file, httpd.conf:

RewriteEngine on
RewriteLog logs/rewrite_log
RewriteLogLevel 1

You may need to twiddle the file name in the RewriteLog directive a bit to conform to your distribution's file system conventions. For example, in the default Apache build, the appropriate directive would be:

RewriteLog var/log/rewrite_log
In addition, you can increase the RewriteLogLevel from 1 up to a maximum of 9 to get more detailed information on what the rewrite engine is doing, which helps in debugging. However, once you are satisfied with the way it is working, it is a good idea to reduce the RewriteLogLevel to 0. This will stop all logging and reduce the load on the Apache server.

Having done this, you are now ready to redirect banner ads. Let us assume you want to redirect all graphics originating from URLs, and I chose to display the “Powered by Apache” logo (which comes with Apache in the file icons/apache_pb.gif) in place of the advertisements. Add the lines shown in Listing 1 to your httpd.conf file to display the Apache GIF instead of images from these sources, where, of course, you replace apache.your.dom with the name of the Apache server you have set up.

Listing 1

As you can see, each redirection consists of two parts: a RewriteCond and a RewriteRule. The RewriteCond identifies the host (%{HTTP_HOST}) we want to redirect from. The third string in the directive is the name of the host, given as a regular expression. In regular expressions, the period (.) stands for any character, so we use "." to denote a literal period in the domain name. ".*" stands for any arbitrary string, and the caret (^) forces a match at the beginning of the host name. Thus, for example, the regular expression ^image.*\.click2com\.net matches the hosts, and, but does not match

Once we have found a host with RewriteCond, we use the following RewriteRule directive to see if any part of the URL matches the first string (regular expression) in the rule; if it does, we redirect to the appropriate (local) graphic. For example, having found a host in the domain in the first RewriteCond, we then check in the RewriteRule which follows it if the string /adanything occurs anywhere in the URL. If it does, we redirect the Apache proxy to send instead the image /icons/apache_pb.gif from the proxy host. Of course, you can use any image you like, and it need not necessarily be on the proxy server host—any URL is fine. The final [R] converts the HTTP request into an HTTP REDIRECT (302). Don't worry if you don't know HTTP; I don't either, but it's required.

You can add as many rewrite conditions and rules as you like to redirect banner ad sites. I'm still looking for a comprehensive listing of well-known ad URLs—let me know if you can add some.


The method of setting up the Apache proxy I have described is purely for the sake of redirecting banner ads. The Proxy module has many other features which are not covered here. Perhaps the most useful of these is its ability to cache often-accessed documents on a local disk, and further reduce bandwidth and time needed to browse the Web. Try playing around with the cache directives in httpd.conf, and judge the effect on your browsing speed.

Also, the method of configuring the rewrite engine I have described here is only one way. This method may not be optimal, as it is the result of one day's fooling around with mod_rewrite, but it works and serves my purpose.

mod_rewrite is an extremely powerful and versatile rewriting engine, and we have seen only one application of its features in this small redirection application. More information on Apache, its modules in general, and mod_rewrite in particular, can be found in Resources.

Another tip you may find useful is to combine the two secondary Apache config files, access.conf and srm.conf, into the primary config file, httpd.conf. This speeds up Apache quite a bit, especially on heavily loaded servers. In order to do this, copy the contents of access.conf and srm.conf into httpd.conf, preferably somewhere at the end, although location doesn't really matter. Now add the following two lines near the beginning of httpd.conf:

AccessConfig    /dev/null
ResourceConfig  /dev/null

Again, location isn't very important, as long as you don't insert those lines into the middle of a multi-line directive.

A rather whimsical description of this approach is available at the Apache site, listed in Resources (see “why are there three config files?”).



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Diflucan info

Gelasia's picture

Hi guys. As a scientist, I am not sure anymore that life can be reduced to a class struggle, to dialectical materialism, or any set of formulas. Life is spontaneous and it is unpredictable, it is magical. I think that we have struggled so hard with the tangible that we have forgotten the intangible.
I am from Botswana and now teach English, give please true I wrote the following sentence: "Making yourself approachable and reasonable, and giving respect to other people is important."

Best regards 8-), Gelasia.