3

I have an Azure CDN with a website origin for my domain and it picked www.mydomain.co.uk automatically (with no option for me to change it). I'd rather not use a Storage origin as I have automatic deployment from Github. So, azxxxxxxx.vo.msecnd.net will retrieve content from www.mydomain.co.uk and cache it locally.

However, I have a URL write rule which will redirect www.mydomain.co.uk to mydomain.com (for various SEO reasons). This will cause requests for CDN content to return a 301 redirect, rather than serving up content to the CDN for it to cache. Is there some way to stop the URL rewrite when content is requested by the Azure CDN?

You're more than welcome to see the code and URL rewrite rules at https://github.com/brentnewbury/PersonalSite/blob/master/web.config

Brent Newbury
  • 373
  • 4
  • 12
  • 1
    For the CDN, you use the /cdn path, can you exclude that path from the URL Rewrite rule? – Erik Oppedijk Oct 24 '14 at 06:58
  • Hi Erik, the code you saw that contains the /cdn is what I eventually came up with. I was going to post it as the answer last night but it was getting a little late. – Brent Newbury Oct 24 '14 at 13:34

1 Answers1

2

For others who might want to know how I solved the issue, I decided to write a URL rewrite rule (see below) that looks for /cdn/ and route that through and no longer process other rules. This seems to work nicely. It means references in my HTML will look like //azxxxxxxx.vo.msecnd.net/cdn/img/pic.jpg for a image that is stored in /img/pic.jpg.

<rule name="CDN Passthrough" stopProcessing="true"> 
   <match url="(.*)(cdn/)([\S]+)" /> 
   <action type="Rewrite" url="{R:1}/{R:3}"/> 
</rule> 
Brent Newbury
  • 373
  • 4
  • 12
  • So you store all your CDN assets under `/cdn/`? I'm running into this same issue. It would be better if Azure CDN knew you had a custom domain enabled and used that. What do you do with slots...? Maybe blob storage is better due to these reasons, not sure. I'm going to try "Custom Origin" for the CDN origin. – kamranicus Jul 31 '15 at 13:28
  • All my assets are stored in folders under the root. At the time, Azure CDN picked the origin domain this has now changed), but the one it picked (.co.uk) had a 301 (to .com) so the assets weren't loading from the CDN. So I added the rule "CDN Passthrough" that will remove the "cdn/" folder, rewriting the URL, and stop processing other rules (the 301 redirect). See https://github.com/brentnewbury/PersonalSite. Hope this helps. – Brent Newbury Jul 31 '15 at 18:04
  • I have it working (I think) with a custom origin (my main domain). However, I'm still confused where the "cdn" folder comes from. If you don't have a CDN folder, does Azure CDN prefix all requests with "cdn/"? e.g. "cdn/images/foo.png"? edit: I see your CDN domain is xxxx.com/cdn, mine isn't, so maybe that's why I was confused. – kamranicus Aug 06 '15 at 18:37
  • The /cdn path would be stripped out by the URL rewrite rules in the web.config. The rule also told IIS not to process other rules would caused a 301 redirect, as Azure CDN picked the wrong domain from my Azure website. But you can now pick a custom origin (as you have discovered). The /cdn path never physically existed. – Brent Newbury Aug 06 '15 at 22:23
  • No files were stored under a /cdn folder. I just needed a way to tell IIS "Hey, when a request comes in from the Azure CDN for http://www.brentnewbury.co.uk/cdn/some-resource, serve up http://www.brentnewbury.co.uk/some-resource and don't bother with the 301 redirect to https://brentnewbury.com I told you to redirect the user to earlier." – Brent Newbury Aug 06 '15 at 22:29
  • I get it now. Because the CDN will follow the URL you give it, you can manage the response it gets. So if in your page you have "me.cdn.net/cdn/file.png" the CDN will request "mysite.com/cdn/file.png" when pulling assets, therefore you can URL rewrite it. Nice! – kamranicus Aug 07 '15 at 17:15