3

I have the Url Rewrite Module installed on my web server that is running Windows Server 2012 R2 at a URL like: http://staging.mysite.net/

I have images, fonts, scripts, etc. loaded into the Azure CDN.

I have specified the following rules in my Web.config:

<rewrite>
    <rules>
        <rule name="CDN: fonts" stopProcessing="true">
            <match url="^fonts/(.*)" />
            <action type="Rewrite" url="http://cdn.staging.mysite.net/fonts/{R:1}" appendQueryString="true" />
        </rule>
        <rule name="CDN: images" stopProcessing="true">
            <match url="^images/(.*)" />
            <action type="Rewrite" url="http://cdn.staging.mysite.net/images/{R:1}" appendQueryString="true" />
        </rule>
        <rule name="CDN: pdf" stopProcessing="true">
            <match url="^pdf/(.*)" />
            <action type="Rewrite" url="http://cdn.staging.mysite.net/pdf/{R:1}" appendQueryString="true" />
        </rule>
        <rule name="CDN: scripts" stopProcessing="true">
            <match url="^scripts/(.*)" />
            <action type="Rewrite" url="http://cdn.staging.mysite.net/scripts/{R:1}" appendQueryString="true" />
        </rule>
        <rule name="CDN: style" stopProcessing="true">
            <match url="^style/(.*)" />
            <action type="Rewrite" url="http://cdn.staging.mysite.net/style/{R:1}" appendQueryString="true" />
        </rule>
    </rules>
</rewrite>

Everything is being returned as a 404 error even though the files exist. For example, my page has a reference to /images/en-us/logo.png. I verified that http://cdn.staging.mysite.net/images/en-us/logo.png actually exists.

In addition, the "Test Pattern" function of the Url Rewrite snap in shows that {R1} returns en-us/logo.png when you enter in either images/en-us/logo.png or /images/en-us/logo.png into the "Input data to test" field.

I momentarily had tried using type="Redirect" instead of type="Rewrite" but this causes a duplicate request/response from the server. The first being a 301, the second being a 200. I would like only the 200 response.

++ Obviously, mysite.net is not really my domain and is being used for example purposes only.

eat-sleep-code
  • 4,753
  • 13
  • 52
  • 98

1 Answers1

6

I'm using the following rule to rewrite all requests for everything under my /assets folder which works:

<rewrite>
  <rules>
    <rule name="Rewrite to CDN">
      <match url="^assets/(.+)$" />
      <action type="Rewrite" url="http://cdn.domain.com/{R:0}" />
    </rule>
   </rules>
</rewrite>
Tom Hall
  • 4,258
  • 2
  • 23
  • 23
  • Weird, the only thing I really see different is that you arent using stopProcessing="true" or appendQueryString="true". I will try removing those from my rules tonight to see if that corrects the problem. – eat-sleep-code Apr 16 '14 at 16:16
  • Also using a slightly different regex for match URL, and on the rewrite URL {R:0} instead of {R:1} – Tom Hall Apr 16 '14 at 21:26
  • Changed mine to match your style, removed stopProcessing="true", removed appendQueryString="true", changed regex to match your pattern and switched URL to be like yours (no folder name and use {R:0}). Still no luck! It still looks like it isnt working. – eat-sleep-code Apr 17 '14 at 03:18
  • Are using the Application Request Routing module? I just found this answer: http://stackoverflow.com/questions/14269149/rewrite-subfolder-to-subdomain-in-web-config/14406762#14406762 – eat-sleep-code Apr 17 '14 at 03:26
  • Yeah, using Application Request Routing, I forgot that 'enable proxy' is disabled by default, as soon as you enable it, it should work straight away. – Tom Hall Apr 17 '14 at 03:55
  • I installed ARR3 using the web platform installer, restarted, do not see any options for it in the iis manager?? – eat-sleep-code Apr 17 '14 at 04:17
  • You've got to look under your server node (top most item on the left hand side tree), not your site, in the IIS Manager, and you'll see the 'Application Request Routing Cache', go into that, then on the right hand side click 'Server Proxy Settings', this is where you can enable the proxy. – Tom Hall Apr 17 '14 at 04:25
  • not even seeing the Application Request Routing Cache in there. I am running IIS 8.5. – eat-sleep-code Apr 17 '14 at 04:27
  • Another question. Write now, if you run page speed or web page test against the site it doesn't know your using a CDN because the rewrite is all happening at the IIS level (super fast!). But is there anyway -- possibly with an outbound rule -- to have the CDN url returned back to these kind of tools? – eat-sleep-code Apr 17 '14 at 05:34
  • Yep absolutely, and to be honest, it might be a better solution. This will help get you started: http://stackoverflow.com/a/17316973/705800 – Tom Hall Apr 17 '14 at 05:46
  • I never got the outbound rules working. I get a 500 error whenever I try to implement the outbound rule. So for now, I am just serving them with the same domain as the rest of my content using an inbound rule and the Application Request Routing. – eat-sleep-code Apr 23 '14 at 20:12