Rewrite the subfolder in the subdomain in web.config

I am trying to write a rewrite rule for the following scenario.

User is trying to download this image:

domain.com/images/folder/picture.jpg 

and instead I need to load:

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

Here is what I have that doesn't work:

 <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: Add more information. Most of the photos are taken from Joomla, so when the root of the domain is something like domain.com, most of the images are entered using src = "/images/folder/picture.jpg" I'm not quite sure how this affects the rewriting, but not one of the options on cheesemacfly does not answer below, work ...

UPDATE2: While the cheesemacfly could not help me in my specific circumstances, I rewarded him with generosity and marked his answer as accepted, because he went higher and further to try to help me chat, I hope his answer will help someone with transcriptions in IIS

+7
source share
2 answers

EDIT:

To be able to rewrite (and not just redirect) URLs to external websites, you need to install the application request routing module and enable proxy mode.

For this:

  • Download and install the module
  • Open the IIS Management Console ( inetmgr )
  • Choose node server
  • Double click on Application Request Routing Cache : ARR
  • Click Server Proxy Settings in the Actions panel (to the right of the screen)
  • Check Enable proxy and click Apply proxy

The second step is to configure your rules.

If you want your rewrite to be path based, 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 a second website, you can simplify the following:

 <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 files ending in a specific extension (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> 

Refer to: http://www.iis.net/learn/extensions/url-rewrite-module/iis-url-rewriting-and-aspnet-routing (section " Which option to use? ")

Tip:

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

At the root of your site -> URL Rewrite -> Create an empty rule -> click a test pattern: Pattern test

If you donโ€™t get the expected result, you can debug your correspondence using the Failover Query Tracking Tool

+5
source

NOTE. Changing the redirection rule instead of overwriting fixes the problem. Ideally, you want this to be a redirect, but I spent many hours trying to get the work to be rewritten, and there are 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> 
0
source

All Articles