Using a .htaccess to Distribute Your Media on a Content Delivery Network

Date Published: 19/05/2009 10:09

When working on a site recently I was asked to move all the media of the site onto a CDN (content delivery network) as the bandwidth usage on the site was far too high. It was a site I built for a well known client as part of a larger advertising campaign (the whole thing included the site, TV ads, magazine ads etc). During the early stages of the campaign it was clear to see the hosting connection could not handle the demands of the site as it was laden with flash animations and high resolution images. Since I had to do it fast I came up with the following solution as a temporary fix for the problem to save me from going through, what is a massive site, and changing all the image paths to use the new CDN we had set up.


Just a quick note to say this is a TEMPORARY fix but is useful never the less. If you are looking to use a CDN because your hosting server cannot handle the CPU usage of your site this is NOT a good solution for you. There are still HTTP requests made to your hosting server which are returned with a 301 message so this should only be used as a temporary fix.

The Important Stuff

Here are some commands you can customise and paste into your htaccess file to carry out the redirects.

RewriteRule .*\.(jpg|gif|png|flv|css|js|swf)$   http://cdn-domain-name/$0   [R=301,L]

How it Works

Your CDN should always have a custom user agent so you can see when it is accessing the original media. These htaccess commands simply 301 redirect any requests for media of types jpg, gif, png, flv, css, js and swf to the CDN domain when the user agent matches that specified on the first line.

When and When Not to Use This

If you have prior knowledge before a build begins that you will be using a CDN this is not solution you should use. Facilitate for changing your media addresses whilst building the site so you can easily point them at the CDN without having to worry about redirecting them. If you site is small or only have a few images, stylesheets, flash files etc I would suggest you take the time to manually work through the site and change the links yourself so not to have the redundant HTTP requests. If your site is of a considerable size, has no issues with CPU usage but is moving over to a CDN to reduce bandwidth usage this is a good temporary solution for you, but you should still look at manually dealing with your links in the long run.


This article is more of a reference than anything else as I feel it is something will probably crop up again at some point due to the nature of my clients. If anyone has any questions or are struggling with a CDN related issue feel free to post below and I'll try to help you out. Please don't post if you are just going to say this isn't the best solution, I know it isn't, its merely a temporary fix for use in emergencies when time is tight.


Sorry comments are currently disabled for maintenence

5 Most Recent Articles

Manually Triggering Events in ASP.NET from JavaScript

A quick guide for ASP.NET developers on how to manually trigger ASP.NET events from JavaScript.

Advanced Use of MySQL Stored Procedures

An article for users of MySQL databases describing how they can use advanced stored procedures to improve efficiently in their applications.

Using MySQL Stored Procedures and Extending MySQLi in PHP

A guide for LAMP developers to using stored procedures in MySQL and extending the MySQLi class.

Reading and Writing to Excel Spreadsheets in Python

An introduction to using the xlwt and xlrd modules for python to interact with Microsoft Excel spreadsheets.

Interact with the Web Using Python and the HTTP Library

This is an introduction to making HTTP requests from a python script/application using httplib.