Skip to content

Instantly share code, notes, and snippets.

@brentnewbury
Created October 24, 2014 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brentnewbury/d9cecf99e3550c464ae9 to your computer and use it in GitHub Desktop.
Save brentnewbury/d9cecf99e3550c464ae9 to your computer and use it in GitHub Desktop.
URL rewrite rule for Azure CDN with Azure Websites origin which avoids interference from other URL rewrite rules when Azure CDN picks the wrong domain
<html>
<head>
<title>Example.com</title>
</head>
<body>
<img src="//azxxxxxx.vo.msecnd.net/cdn/img/example.png" /> <!-- CDN will receive a HTTP 200 with the image content, thanks to the "CDN Passthrough" rewrite rule -->
<img src="//azxxxxxx.vo.msecnd.net/img/example.png" /> <!-- Normal Azure CDN URI which will recieve a 301 redirect if Azure CDN chooses www.example.com as the origin domain-->
<img src="/img/example.png" /> <!-- Non CDN edge cached content -->
</body>
</html>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="CDN Passthrough" stopProcessing="true">
<match url="(.*)(cdn/)([\S]+)" />
<action type="Rewrite" url="{R:1}/{R:3}"/>
</rule>
<rule name="Remove WWW - Causes 301 redirect affecting the Azure CDN requests if it choses www.example.com as the origin domain" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
</conditions>
<action type="Redirect" url="http://{C:2}{PATH_INFO}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment