Question

I can't seem to get my head around these rewrite rules for some reason and I was hoping you guys could help. What I want is an outbound rule that will rewrite paths for link, img, script, and input tags.

I want to change this: http://www.mysite.com/appname/css/file.css

To this: http://cdn.mysite.com/css/file.css

So, basically I need to swap the host name and drop the app name from the URL. I've got the pre-condition filters to *.aspx files set already, but the rest seems like Greek to me.

EDIT for clarity

The appname in the URL above is an application in IIS. It's a placeholder for whatever appname happens to be in use. It could be any of over 50 different apps with our current setup. There will ALWAYS be an appname. Perhaps that will make the rule even easier.

The hostname, in this case www.mysite.com, can also vary slightly in terms of the subdomain. It might be www1.mysite.com, www2, etc. Also, just realized that I need to maintain the SSL if there.

So, I guess when it comes down to it, I really just need to take the URL, minus the appname, and append it to the new domain, while respecting the protocol that was used.

Original URL: http(s)://{host}/{appname}/{URL}

Output: http(s)://cdn.mysite.com/{URL}

Was it helpful?

Solution

I assume your website domain is always the same, then this rule should do:

<rule name="CdnRule" preCondition="OnlyAspx" >
    <match filterByTags="Img, Input, Link, Script" pattern="^(.+)://.+?\.(.+?)/.+?/(.*)" />
    <action type="Rewrite" value="{R:1}://cdn.{R:2}/{R:3}" />
</rule>

<preConditions>
<preCondition name="OnlyAspx">
    <add input="{PATH_INFO}" pattern=".+\.aspx$" />
</preCondition>
</preConditions>

EDIT: changed according to clarified question

I assume the subdomain (www, www2, ...)is always there and it has to be ignored in target url.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top