2

I am trying to setup URL rewriting to rewrite all references to images, etc. to point to my CDN.

I have it working on any reference within the .NET pages using the following web.config settings:

<outboundRules>
    <rule name="Outbound CDN: fonts" preCondition="IsTextContent">
    <match filterByTags="None" pattern="^/fonts/(.+)$" />
        <action type="Rewrite" value="http://cdn.mydomain.com{R:0}" />
    </rule>
    <rule name="Outbound CDN: images" preCondition="IsTextContent">
        <match filterByTags="A, Img, Link" pattern="^/images/(.+)$" />
        <action type="Rewrite" value="http://cdn.mydomain.com{R:0}" />
    </rule>
    <rule name="Outbound CDN: pdf" preCondition="IsTextContent">
        <match filterByTags="A, IFrame" pattern="^/pdf/(.+)$" />
        <action type="Rewrite" value="http://cdn.mydomain.com{R:0}" />
    </rule>
    <rule name="Outbound CDN: scripts" preCondition="IsTextContent">
        <match filterByTags="Script" pattern="^/scripts/(.+)$" />
        <action type="Rewrite" value="http://cdn.mydomain.com{R:0}" />
    </rule>
    <rule name="Outbound CDN: style" preCondition="IsTextContent">
        <match filterByTags="Link" pattern="^/style/.+\.(?:css|less|sass|master|cs)$" />
        <action type="Rewrite" value="http://mydomain.com{R:0}" />
    </rule>
    <preConditions>
        <preCondition name="IsTextContent">
            <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/(.+)$" />
        </preCondition>
    </preConditions>
</outboundRules>

However, I can not figure out to rewrite references to the images and fonts referenced within CSS files.

Does anyone have any ideas on how to do a similar URL rewrite for images and fonts referenced within the CSS.

I am using Windows Server 2012R2, IIS 8.5, .NET 4.5.1, and the latest version of the URL Rewrite and Application Request Routing modules.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
eat-sleep-code
  • 4,753
  • 13
  • 52
  • 98
  • Any particular reason for using web.config in lieu of HTTP module/handler, application routes, or IIS-driven redirects here? –  May 08 '14 at 03:29
  • I am using the Url Rewrite module, which is a configurable IIS-level module designed for exactly this purpose. An IIS redirect will result in a 301 response which is a performance hit. That means there are two responses for every request... the 301 response for your first request to the original URL, and a 200 response for the request to the CDN URL. This is a lot of HTTP traffic overhead, which would negate any advantage of leveraging a CDN. – eat-sleep-code May 08 '14 at 13:15
  • Ah, I meant "rewrites" not "redirects". The Url Rewrite module is poorly documented (relatively) and lacks the power of mod_rewrite but does have the advantages of not invoking the managed runtime stack. In any case I wasn't suggesting you 301/302 redirect to your CDN. I also don't think rewriting to your CDN is a good idea, for the same reason redirecting isn't. You really want to change those references at the source HTML level so they don't hit your site at all. See comments to http://stackoverflow.com/questions/8285216/cdn-related-rewrite-image-urls-automatically-from-htaccess etc. –  May 08 '14 at 14:19
  • Hard coding the CDN url would become a managibility nightmare. We have a staging site and a production site. We would need to code every image, script, font, etc. throughout the site with a variable, as well as have disparate CSS files, etc. – eat-sleep-code May 08 '14 at 17:43
  • A nightmare you can address at build time, by generating the appropriate CDN URLs as a formal step in your build process. Nevermind that as it's not possible in your case. Anyway, the browser will issue a request for each linked resource in the CSS. Those should be be caught by your rewrite rules just like any other request. –  May 08 '14 at 18:39

0 Answers0