【问题标题】:Azure app-service non-www to www redirect rule to ignore the cdn domainAzure 应用服务非 www 到 www 重定向规则以忽略 cdn 域
【发布时间】:2017-11-29 21:17:56
【问题描述】:

non-wwwwww.sitedomain.com 的重定向规则有效,但 CDN 域请求的免责条款(add input=" {HTTP_HOST}" pattern="cdnprefix.azureedge.net" negate="true") 被忽略,cdn 请求也重定向到 www.sitedomain.com。你能帮我修改重写部分来解决这个问题吗?

<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>

加上有效的 HTTP -> httpS 重定向规则(拆分以简化 CDN 豁免规则的解决方案),它看起来像这样:

<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>

【问题讨论】:

    标签: asp.net-mvc azure iis web-config url-redirection


    【解决方案1】:

    对于规范域,你只需要这条规则

    <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>
    

    然后你可以为CDN添加下一部分

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

    希望这行得通!

    【讨论】:

    • 试过了,没有任何改善。规范规则有效,但 cdn 资源重定向到站点域 cdnprefix.azureedge.net/path2resource -> www.sitedomain.com/path2resource
    • 这是我添加的规则,对我来说效果很好。在我的本地测试 sitedomain.com{HTTP_URL}" appendQueryString="false" redirectType ="永久" />
    • 既然您已经给出了永久重定向,请您彻底清除浏览器缓存并重试。
    • 您修改后的答案在最后一行有点偏离,它应该是“www.sitedomain.com{HTTP_URL}”而不是“sitedomain.com。无论如何,我尝试使用不同的浏览器并使用 chrome 上的开发工具,没有缓存选项和相同的结果。 IIS 和 Azure 的不同版本之间可能存在不同的行为。
    • 上次试用后,我还尝试通过删除规范部分来测试 CDN 请求是否会通过并且有效,但我们需要规范名称强制执行。
    猜你喜欢
    • 2023-01-30
    • 1970-01-01
    • 1970-01-01
    • 2017-05-09
    • 1970-01-01
    • 2019-02-04
    • 2011-02-06
    • 2023-03-29
    • 1970-01-01
    相关资源
    最近更新 更多