Site Moved

This site has been moved to a new location - Bin-Blog. All new post will appear at the new location.


Practical Uses for mod_rewrite

I have explained how to use mod_rewrite to create structured URLs and about the most used mod_rewrite directives. Now to see some practical use of mod_rewrite in various situations.

URL Change

Good URLs don't change - but sometimes you have to do it. Move one file from its previous location to a new one. Even I have done it - I moved my site from to Bin-Co, OpenJS and BinnyVA. When you make such a move, you want to make sure that your visitors move with you. This can be done using mod_rewrite.

Lets say you are moving the a page from to Now when ever someone visits the oldfile.html file, they get a 404 error. You can use the following script to make sure that all visitors to the old page is redirected to the new one...

RewriteRule ^oldfile\.html$ [R]

Escape the Slashdot Effect

Slashdot Effect is a situation when a link to a smaller site appear in a heavy traffic site like Slashdot or Digg. Due to the huge traffic this brings, many site with low bandwidth will crumble. One method to prevent this is to deny access to all visitors who have come from the high traffic site(let us assume it is There are other methods also(eg. mirroring). So you have two requirements...

  • Deny access to all from
  • Allow access to all others.

RewriteCond %{HTTP_REFERER} ^http://slashdot\.org [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://www\.slashdot\.org [NC]
RewriteRule ^.*$ /denied.html [F]

The logic behind the above code goes somewhat like this...

RewriteCond %{HTTP_REFERER} ^http://slashdot\.org [NC,OR] - If the visitor have come from OR,
RewriteCond %{HTTP_REFERER} ^http://www\.slashdot\.org [NC] - if he have come from
RewriteRule ^.*$ /denied.html [F] - Show a 'Forbidden' message.

Prevent Image Hot Linking

Hot Linking is when someone links to a file from our server directly in a way that our bandwidth will be used when anyone visits the other person's site. For example, let say we have a cool picture called, say, 'cool_picture.jpg'. We have kept it in ''. Now some guy from another site, likes this picture and decided to show the image in his site - so he uses the HTML code...

<img src="" />

So whenever someone visits this guys site, the image is fetched from our server - thus increasing our bandwidth costs. For this reason, this method is also known as bandwidth theft.

So how do we prevent it? The same method used in the last situations - look at the referrer, and if it is not ours, show a forbidden message.

RewriteCond %{HTTP_REFERER} !^$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www\.our-domaincom/\.org [NC]
RewriteCond %{HTTP_REFERER} !^http://our-domain\.com/\.org [NC]
RewriteRule ^.*$ /denied.html [F]

Using Different Files for Different Browsers

As all good web designers know, different browsers have different ways of parsing the same CSS file. So your masterpiece in IE will look like something the cat brought in Firefox. One simple way of solving this problem is to use mod_rewrite and provide a different css file based on the visitor's browsers. Lets say we have a CSS file called 'style.css'. But for IE, we have created a special CSS file called 'style_ie.css'. For firefox, we have yet another CSS file called 'style_ff.css'. Our pages uses the HTML code...

<link href="style.css" type="text/css" rel="stylesheet" />

Now to provide different files based on the user agent of the visitor...

RewriteCond %{HTTP_USER_AGENT} ^Mozilla/(.*)MSIE
RewriteRule (.*)style.css$ $1style_ie.css [L]

RewriteCond %{HTTP_USER_AGENT} ^Mozilla/(.*)Firefox
RewriteRule (.*)style.css$ $1style_ff.css [L]

For more solutions to situations like this, see the URL Rewriting Guide from Apache Docs.