1
votes

The redirect rule below of non-www to www.sitedomain.com works, but the exemption clause for the CDN domain requests (add input="{HTTP_HOST}" pattern="cdnprefix.azureedge.net" negate="true") are ignored and the cdn requests redirect as well to www.sitedomain.com. Can you help me modify the rewrite section(s) to solve that?

<rule name="Redirect non-www to www.sitedomain.com" stopProcessing="true">
  <match url="(.*)" />
  <conditions logicalGrouping="MatchAll">
    <!-- domain is not canonical  -->
    <add input="{HTTP_HOST}" matchType="Pattern" ignoreCase="true" pattern="^sitedomain\.com$" />
    <add input="{HTTP_HOST}" pattern="^www\.sitedomain\.com$" negate="true" />
    <add input="{HTTP_HOST}" pattern="cdnprefix\.azureedge\.net" negate="true" />
  </conditions>
  <action type="Redirect" url="https://www.sitedomain.com{HTTP_URL}" redirectType="Permanent" appendQueryString="false" />
</rule>

Together with the effective HTTP -> httpS redirect rule (split up to simplify solution for CDN exemption rule), it looks like this:

<rule name="Redirect to https">
  <match url="(.*)" />
  <conditions>
    <add input="{HTTPS}" pattern="Off" ignoreCase="true" />
    <add input="{REQUEST_METHOD}" pattern="^get$|^head$" />
  </conditions>
  <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Redirect non-www to www.sitedomain.com" stopProcessing="true">
  <match url="(.*)" />
  <conditions logicalGrouping="MatchAll">
    <!-- domain is not canonical  -->
    <add input="{HTTP_HOST}" matchType="Pattern" ignoreCase="true" pattern="^sitedomain\.com$" />
    <add input="{HTTP_HOST}" pattern="^www\.sitedomain\.com$" negate="true" />
    <add input="{HTTP_HOST}" pattern="cdnprefix\.azureedge\.net" negate="true" />
  </conditions>
  <action type="Redirect" url="https://www.sitedomain.com{HTTP_URL}" redirectType="Permanent" appendQueryString="false" />
</rule>
1

1 Answers

1
votes

For canonical domain,you only need this rule

<rule name="CanonicalHostNameRule1" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{HTTP_HOST}" pattern="^www.\sitedomain\.com$" negate="true" />
                    </conditions>
                    <action type="Redirect" url="https://www.sitedomain.com/{R:1}" />
                </rule>

Then You can add the next section for CDN

<add input="{HTTP_HOST}" pattern="cdnprefix\.azureedge\.net" negate="true" />

Hope this works!