amateur
amateur

Reputation: 44673

setting up cookieless domain for static resources

I am running an asp.net web application on IIS7 with .net 3.5.

To improve my Yslow score I am looking at implementing a cookieless domain for my static resources such as images, CSS and JavaScript.

My site's URL is www.mywebsite.com.

So static resources will for example have a URL of static.mywebsite.com/styles.css

I would like to make this change as seamless as possible. I use relative paths throughout the site.

I can set up the subdirectoy static.mywebsite.com

But I also need to make the changes to my application. I am looking for help with this. With the new functionality that can be included in the web.config for URL rewriting. Any tips or ideas as to how I may be able to set up static.mywebsite.com for images/css/javascript?

Upvotes: 1

Views: 508

Answers (1)

Victor Leontyev
Victor Leontyev

Reputation: 8736

It is possible with outbound rules. This rules will rewrite js,css,jpg and png to static.mywebsite.com.

<outboundRules rewriteBeforeCache="true">
    <rule name="CDN-01-css" preCondition="CheckHTML" stopProcessing="true">
      <match filterByTags="Link" pattern="/(.*\.css)" />
      <action type="Rewrite" value="http://static.mywebsite.com/{R:1}" />
    </rule>
    <rule name="CDN-01-js" preCondition="CheckHTML" stopProcessing="true">
      <match filterByTags="Script" pattern="/(.*\.js)" />
      <action type="Rewrite" value="http://static.mywebsite.com/{R:1}" />
    </rule>
    <rule name="CDN-01-jpg" preCondition="CheckHTML" stopProcessing="true">
      <match filterByTags="Img" pattern="/(.*\.jpg)" />
      <action type="Rewrite" value="http://static.mywebsite.com/{R:1}" />
    </rule>
    <rule name="CDN-01-png" preCondition="CheckHTML" stopProcessing="true">
      <match filterByTags="Img" pattern="/(.*\.png)" />
      <action type="Rewrite" value="http://static.mywebsite.com/{R:1}" />
    </rule>
    <preConditions>
      <preCondition name="CheckHTML">
        <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
      </preCondition>
    </preConditions>
</outboundRules>

For example:

It will automatically change your output html

<link rel='stylesheet' id='social-logos-css' href='/wp-content/plugins/jetpack/_inc/social-logos/social-logos.min.css?ver=1' type='text/css' media='all' /> to

<link rel='stylesheet' id='social-logos-css' href='http://static.mywebsite.com/wp-content/plugins/jetpack/_inc/social-logos/social-logos.min.css?ver=1' type='text/css' media='all' />

Upvotes: 0

Related Questions