Tutorial :The Logistics Side of Using Multiple Sub Domains for Static Content


It's generally considered a best practice to serve static content, such as images and css, from different sub domains (images1.domain.com, images2.domain.com, etc). I've seen this discussed in detail in various places, however I'm concerned about the general logistics of this in terms of maintainability.

Our site has thousands of pages and changing all references to static content from relative to absolute links pointing to different sub domains sounds like a nightmare, what are some general approaches to accomplishing this? Is there any way to automate this, perhaps with a server that sits in front of the web server and rewrites incoming requests to point to different sub domains? Are there any frameworks built with this in mind? Is the performance gain worth the added complexity and maintenance costs?

EDIT: Our environment is ASP.NET 3.5 SP1 on IIS 7.


I did this once on the small scale using mod_rewrite (e.g. one Google link: http://corz.org/serv/tricks/htaccess2.php)

The reason I chose this is because I had certain file extensions that were all on the separate server. Anything with that extension moved straight over to the same file structure on the subdomain. Might not be the same situation you have.

This is a small scale solution, not sure of the performance implications of doing this for thousands. For that, I might try a script for textual search/replace.


Ewww!! Sounds like highly-premature optimization to me. Unless you benchmark this carefully, I'd put this 'improvement' last on my to-do list.

If you really want to do that, you should add an extra layer of indirection while generating your HTML (pseudo-JSP syntax follows). Instead of:

<img src="http://images.mysite.com/image/image1.gif"/>  


<img src="${image:url(image1.gif)}"/>  

and have image:url generate a random virtual host or something; you can at a later date redefine how image:url works.

If this is not possible, I believe Eran Kampf's answer is the right one; in Java-parlance you would use a ServletFilter.


I did this once but luckily the people before me hadn't been using relativel links, they had used a constants file (for the most part) so it was relatively easy.

I think it's a good idea to do, it will open up your options for scalability. Perhaps search you code base for .jpg and .gif and create a macro to append the subdomain to the root of the path. Be sure to create a single function that returns the subdomain so you can house the logic for which subdomain to choose in a central location.


One possible approach might be to use a client-side Javascript (jQuery?) to go over all the relevant links and change them to point to the static location.

Since you want to use several domains without breaking browser cache I'd use some hashing on the filename to make sure that name always points to the same subdomain...

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »