title: URL Rewrite on Windows 2016 (IIS 10) with .Net Core to point to CDN
body: <p>I have a brand new virtual server running Windows 2016 (IIS 10).   I have a website that is built with .NET Core 1.1.</p>
  
  <p>I would like to rewrite all image, js, font, and css references to point to a CDN.</p>
  
  <p><strong>Problem 1</strong>, adding this rule:</p>
  
  <pre><code>&lt;rule name="CDN Assets" stopProcessing="true"&gt;
      &lt;match url="^css/(.+)$|^images/(.+)$|^fonts/(.+)$|^js/(.+)$" ignoreCase="true" /&gt;
      &lt;action type="Rewrite" url="https://cdn.example.net/{R:0}" appendQueryString="true" /&gt;
  &lt;/rule&gt;
  </code></pre>
  
  <p>... results in a 404 for every request.   For example a request to <a href="https://example.net/images/en-us/logo.png" rel="nofollow noreferrer">https://example.net/images/en-us/logo.png</a> even though the image exists both in the real folder under and at <a href="https://cdn.example.net/images/en-us/logo.png" rel="nofollow noreferrer">https://cdn.example.net/images/en-us/logo.png</a></p>
  
  <p>Is this possibly caused by .NET Core or by HTTP2 (which is introduced in IIS 10).   Or do I need some sort of outbound rule also?</p>
  
  <p><strong>Problem 2</strong>, adding an outbound rule like:</p>
  
  <pre><code>&lt;rule name="Outbound CDN: images" preCondition="IsHtml"&gt;
      &lt;match filterByTags="A, Img, Link" pattern="^/images/(.+)$" /&gt;
      &lt;action type="Rewrite" value="//cdn.example.net{R:0}" /&gt;
  &lt;/rule&gt;
  &lt;preConditions&gt;
      &lt;preCondition name="IsHtml"&gt;
          &lt;add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html"/&gt;
      &lt;/preCondition&gt;
  &lt;/preConditions&gt;
  </code></pre>
  
  <p>Causes the page response to become complete mangled unreadable garbage.   Is this because of the response is sent over HTTPS?</p>
  
  <hr>
  
  <p><strong>UPDATE (2016/03/16 8:01PM MST)</strong></p>
  
  <p>Using @EricB suggestion I have tried implementing it via the Microsoft.ASPNetCore.Rewrite package to no avail.</p>
  
  <p>I have this in my <em>Startup.cs</em> and just the local images are being loaded:</p>
  
  <pre><code>string CDNroot = Configuration["URLs:CDN"].Trim();
  RewriteOptions rewriteOptions = new RewriteOptions()
      .AddRewrite(@"^(css|images|fonts|js)(.+)$", CDNroot + "/$1$2", skipRemainingRules: true);
  app.UseRewriter(rewriteOptions);
  </code></pre>
  
  <p>If I change this to:</p>
  
  <pre><code>string CDNroot = Configuration["URLs:CDN"].Trim();
  RewriteOptions rewriteOptions = new RewriteOptions()
      .AddRedirect(@"^(css|images|fonts|js)(.+)$", CDNroot + "/$1$2");
  app.UseRewriter(rewriteOptions);
  </code></pre>
  
  <p>... and request an image URL it is correctly <em>redirected</em> to the CDN URL.   But you wouldn't want a redirect, as that is adding an extra 30x response to every asset request.   I am stumped why Redirect works, but Rewrite doesn't.</p>