Saving Bandwidth in servers

About a year ago, we noticed that we were running up our regular quota of one-month usage of bandwidth within 15 days. One thing about our portal is we have lavish illustration or images adorning every page. Most of the images and flash files are systematically optimized for maximum resolution with minimum file size with the specific intent on keeping our average page loading time to manageable level for the end users.

Images and flash files consumed about 58-65 % of our total bandwidth and any savings in this region would help our cause. But as I mentioned earlier, things appeared to be going out of control for a time. We were getting lots of hits from hundreds of other sites hot-linking to our images and we ended up paying for the additional bandwidth consumption for that month.

Fortunately, as our server is Apache – the most popular HTTP server on the planet, modifying the behavior of our server is easy. Setting server directives tailor made for each directory is quite easy with simple text directives in a .htaccess file. Apache server’s mod_rewrite module is called as the Swiss Army Knife for a number of reasons. You can create a complete bomb-proof web application with little more than a few lines of PHP/Perl code strung together with a carefully set of instructions deployed in a .htaccess file.

It is decided that only pages from our server can use the accompanying images and any other site linking to our images will be served a simple small image – about 6 k, with our site name embedded.

Image to save server bandwidth

We deployed the following directives in the .htaccess file in the image directory:

All the directives shown below are populated with comments starting with a # symbol.


RewriteEngine on # Invokes mod_rewrite module

RewriteCond %{HTTP_REFERER} !^$ [NC] # identify the referer

RewriteCond %{HTTP_REFERER} !^*$ [NC]

RewriteRule .*\.(jpg|jpeg|gif|png|bmp)$ [R,NC]


The last directive indicates to the server that it must serve the default image.jpg for other sites directly linking to the images from this directory.

Most webmasters who directly link to our images are not aware of the implications of the bandwidth consumption for us. This method ensures that there are no ruffled feathers as a consequence. There is a flip side to this – if someone views the page from a search engine’s cache, they will be mildly amused by a single image in various sizes adorning our own pages. But that is small price to pay for avoiding a convoluted solution for the prevention of hot-linking.

Leave a Reply

Our Parent site TargetWoman - the leading women portal presents painstakingly researched extensive information in the form of thousands of condensed pages. It offers the widest and the most detailed information on subjects women care.