9

I'm attempting to write a rewrite rule for the following scenario.

User attempts to load this picture:

domain.com/images/folder/picture.jpg

and instead, I need it to load:

cdn.domain.com/images/folder/picture.jpg.

Here's what I have that isn't working:

<rule name="CDN rewrite for Images">
    <match url="(.*)" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="domain.com" />
        <add input="{REQUEST_URI}" pattern="^/images/folder/(.*)$" />
    </conditions>
    <action type="Rewrite" url="cdn.domain.com/images/folder/{C:1}" />
</rule>

UPDATE: Adding additional info. Most pictures are being served up from Joomla so while the root of the domain is something like domain.com, most images are input with a src="/images/folder/picture.jpg" Not quite sure how this is affecting the rewrite, but none of the options on cheesemacfly's answer below, are working...

UPDATE2: While cheesemacfly was unable to help me in my particular circumstances, I awarded him the bounty and marked his answer as the accepted one because he went above and beyond to try to help me in chat. Hopefully his answer will help someone with rewrites on IIS.

GregD
  • 6,860
  • 5
  • 34
  • 61
  • The pattern `^/images/stories/catalogue/(.*)$` doesn't match your entry `domain.com/images/folder/picture.jpg`. Is that a typo? What you want to do here is redirect any request like `domain.com/images/folder/*` to `cdn.domain.com/images/folder/*` keeping the requested filename/path, am I right? – cheesemacfly Jan 18 '13 at 16:10
  • It was a typo. Corrected above. And yes, you are correct about the rewrite. I am using origin-pull with my cdn – GregD Jan 18 '13 at 19:10
  • Sorry I couldn't help you more...I'd love to see the solution if you find it! – cheesemacfly Jan 25 '13 at 01:12
  • I have updated my answer with what I found lately. Let me know if it helps... – cheesemacfly Jan 31 '13 at 18:34

2 Answers2

12

EDIT:

To be able to rewrite (and not only redirect) urls to outside websites, you need to install the Application Request Routing module and enable the proxy mode.

To do so:

  1. Download and install the module
  2. Open your IIS management console (inetmgr)
  3. Select Your server node
  4. Double click on Application Request Routing Cache: ARR
  5. Click on Server Proxy Settings on the Actions pane (right of the screen)
  6. Check the box Enable proxy and click on Apply proxy

The second step is about setting up your rules.

If you want your rewrite to be based on the path then use the following code:

<rewrite>
  <rules>
    <rule name="Rewrite to cdn domain">
      <match url="^images/folder/(.+)$" />
      <action type="Rewrite" url="http://cdn.domain.com/images/folder/{R:1}" />
    </rule>
   </rules>
</rewrite>

Or if you keep the same folder architecture on the second website you can simplify as follow:

<rewrite>
  <rules>
    <rule name="Rewrite to cdn domain">
      <match url="^images/folder/(.+)$" />
      <action type="Rewrite" url="http://cdn.domain.com/{R:0}" />
    </rule>
   </rules>
</rewrite>

If you want to catch only the files ending with a specific extension (let's say images):

<rewrite>
  <rules>
    <rule name="Forward to cdn domain">
      <match url="^images/folder/.+\.(?:jpg|bmp|gif)$" />
      <action type="Rewrite" url="http://cdn.domain.com/{R:0}" />
    </rule>
  </rules>
</rewrite>

Please refer to: http://www.iis.net/learn/extensions/url-rewrite-module/iis-url-rewriting-and-aspnet-routing (section "Which Option Should You Use?")

TIP:

The best way to test your pattern is to use the IIS test pattern tool.

At the root of your website -> URL Rewrite -> Create a blank rule -> click on test pattern: Pattern test

If you don't get the expected result, you can debug your rewrite using the Failed Request Tracing tool

cheesemacfly
  • 11,622
  • 11
  • 53
  • 72
  • None of your variations have worked. When I use any of them, it "breaks" the pictures. When I look at the source code, because some of this comes from Joomla, a picture has src="/images/folder/picture.jpg" as a for instance. – GregD Jan 23 '13 at 17:54
  • You lost me a bit...You have a Joomla website using images hosted on your IIS7? Where is this Joomla website hosted? – cheesemacfly Jan 23 '13 at 19:53
  • It's a Joomla install on IIS 7 – GregD Jan 23 '13 at 20:02
  • And using one of the solution above, if you call `domain.com/images/folder/picture.jpg` in your browser does it redirect you to `cdn.domain.com/images/folder/picture.jpg`? – cheesemacfly Jan 23 '13 at 20:10
  • That is correct. The images "break" and are no longer visible once I save the web.config. If I try to visit the image URL in the browser, it returns a 404 – GregD Jan 23 '13 at 20:22
  • What do you mean by the images "break"? Is your `cdn.domain.com` up and running and having the `/images/folder/picture.jpg` available? – cheesemacfly Jan 23 '13 at 20:27
  • The images will be there prior to saving any of the rules that you gave as examples in the web.config. When I make any of the changes that you give as examples, then the pictures are replaced with placeholder images and the URLs of the images return 404 errors. The CDN is up and running fine..all the images exist and I'm doing an origin-pull for them. – GregD Jan 23 '13 at 20:36
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/23256/discussion-between-cheesemacfly-and-gregd) – cheesemacfly Jan 23 '13 at 20:39
1

NOTE: Changing the rule to be a redirect instead of a rewrite fixes the problem. Ideally you want it to be a redirect but I have spent many hours trying to get the rewrite to work, and so far no solutions yet.

<rule name="Rewrite to images.cdn.com" enabled="true" stopProcessing="true">
<match url="images/(.+)$" ignoreCase="true" />
<action type="Redirect" url="http://images.cdn.com/{R:1}" />
</rule>
fseminario
  • 801
  • 1
  • 9
  • 13