简体   繁体   中英

Redirect *azurewebsites.net to .com but not for CDN

I have a Joomla site on Azure. I have set up the .com properly and I understand why the *azurewebsites.net URL is still working.

I wanted to redirect all traffic from *azurewebsites.net to the .com. I followed this guide and it works as intended. http://onthecloud.azurewebsites.net/seo-tip-how-to-block-the-.azurewebsites.net-domain

However, I also have an Azure CDN in place (mycdn.azureedge.net/). I use JCH Optimize to set up the CDN. I see the the calls to mycdn.azureedge.net/ are generated properly. However, somehow, because of the web.config redirect, all the traffic is redirected from the CDN back to my .com, which defeats the purpose of the CDN. This creates a lot of redirect calls.

Is there a way to write the web.config lines to exclude the CDN?

Here's what my web.config looks like. When I delete those lines, the CDN redirects disappear, so the problem is really here.

<rule name="Disable Azure Domain" patternSyntax="Wildcard" stopProcessing="true">
  <match url="*" />
  <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
    <add input="{HTTP_HOST}" pattern="*.azurewebsites.net" />
  </conditions>
  <action type="Redirect" url="http://www.example.com{REQUEST_URI}" redirectType="Permanent" />
</rule>

Look at your conditions. You're filtering all traffic that goes to "*.azurewebsites.net". All calls to your cdn fall into that filter as well, which is why you're seeing them be redirected:

<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
  <add input="{HTTP_HOST}" pattern="*.azurewebsites.net" />
</conditions>

Instead, filter the calls just to your website. So your condition would look more like:

<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
  <add input="{HTTP_HOST}" pattern="yoursite.azurewebsites.net" />
</conditions>

That'll let calls to the CDN go through just fine.

You can find more details on setting up redirect rules for your Azure Web App here: http://zainrizvi.io/2016/04/07/block-default-azure-websites-domain/

Fyi, below is the full web.config that I use for my site. You'll not that I've set some of the other settings a bit differently. Try using that web.config file instead (just change the site names as appropriate) and see if it works for you.

<configuration>
  <system.webServer>  
    <rewrite>  
      <rules>  
        <rule name="Redirect to zainriziv.io" stopProcessing="true">
          <match url="(.*)" />  
          <conditions logicalGrouping="MatchAny">
            <add input="{HTTP_HOST}" pattern="^zainrizvi\.azurewebsites\.net$" />
          </conditions>
          <action type="Redirect" url="http://www.zainrizvi.io/{R:0}" />  
        </rule>  
      </rules>  
    </rewrite>  
  </system.webServer>  
</configuration>  

Note: When you're testing the fix your browser might have cached the redirect, so you might want to try fiddler or a new browser/pc to verify things work as expected

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM