首页 > 解决方案 > Azure 将非 www 重定向到 www

问题描述

我对 Azure 重定向有疑问。

我搜索了其他主题,但不是我的问题。

我为此配置了 ASP.NET Core 2.2 项目和 Azure。我也有指向 Azure 的域。我已将 www.example.com 格式的域添加到 azure,它也适用于 https。

example.com 格式出现问题

我想将 example.com 格式重定向到 www.example.com URL

我设法用这个来完成这项工作,但时间很短。我使用这个 web.config 进行重定向

网络配置

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <rewrite>
      <rules>
        <rule name="CanonicalHostNameRule" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^example.com$" />
          </conditions>
          <action type="Redirect" url="https://www.example.com/{R:0}" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

奇怪的是 Google Chrome 和 Opera 重定向到 www 域,而不是 Edge 和 Firefox。然后当我清除缓存时,它不会重定向。

似乎 Azure 喜欢重写一些东西。

感谢帮助!!

标签: asp.netazure

解决方案


除了 web.config 文件更改之外,请确保 Azure 网站实例包含该区域内的正确域绑定Hostname bindings

<rule name="Redirect to www">
  <match url=".*" />
  <conditions logicalGrouping="MatchAny">
    <add input="{HTTP_HOST}" pattern="^example.com$" negate="true" />
  </conditions>
  <action type="Redirect" url="http://www.example.com/{R:0}" redirectType="Permanent"/>
</rule>

推荐阅读