Recently a colleague of mine wrote an article on setting up a CDN (Content Delivery Network). This made me experiment a bit with some IIS 8 settings. It is possible to set up IIS to rewrite all outgoing requests in such a way that your static content (e.g. images, style-sheets, etc) will be downloaded from a separate location. The main benefit of this method is (in my opinion) that it doesn’t requiren any adjustments on your target site. All it requires is some tweaking of your web.config and ofcourse a working CDN.

Outbound rewrite rules are a pretty powerful thing, when used properly. When used incorrectly, they can slow your site down to a screeching halt. Lucky for us, the outbound rewrite rules can be used to filter out specific requests. In this case we will use them to rewrite all outgoing HTML code. We want to search out all static content (in this example .jpg and .png files) and rewrite it on the fly to be downloaded from a CDN. So, first we want to set the proper precondition. Loads of data leaves a webserver, so let’s make sure we only look at the relevant parts of it. We start by setting up a precondition:

<preConditions>

      <preCondition name="CheckHTML"> 

           <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" /> 

      </preCondition> 

</preConditions> 

Now we’re looking at all HTML only it’s time to get even more specific. Outbound rules allow you to filter a tag you want to look for. In this case we assume all images are located in tags. So we can use the following filtering before actually matching it to a pattern:

<rule name="CDN"stopProcessing="false">  
      <match filterByTags="Img" pattern="(https?:\/\/www.(.*)\.(jpg|png))" />  
      <action type="Rewrite" value="http://cdn.{R:2}.{R:3}" />  
 </rule> 

Noticed the ugly regex I used there? Let’s take a look at it:

(https?:\/\/www.(.*)\.(jpg|png))

All this baby does is matching all url’s containing .jpg or .png on the end. So basically it makes sure we catch all outgoing image url’s before they reach the browser. This same regex gives us 3 backreferences. We only need two of them: {R:2} and {R:3}. These contain (in my case) the stripped url (no more http://www.) and the extension. We use these later on to rebuild the url for our CDN. You might want to adjust this regular expression to suit your specific needs.

For me: this one is all I need. Now it’s time for rewriting our url. I can use the backreferences that are given to re-assemble the image location to something that should be working on the CDN. How the data gets there is up to you. I’m only pointing out how you can redirect traffic there ;) After your done, the following can be used in your web.config:

<rewrite> 

      <outboundRules>

          <rule name="CDN" preCondition="CheckHTML" stopProcessing="false"> 

                <match filterByTags="Img" pattern="(https?:\/\/www.(.*)\.(jpg|png))" /> 

                <action type="Rewrite" value="http://cdn.{R:2}.{R:3}" /> 

           </rule> 

           <preConditions> 

                <preCondition name="CheckHTML"> 

                     <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" /> 

                </preCondition> 

          </preConditions> 

      </outboundRules>

</rewrite>