wwwless 站点使用 url 重写 iis 8.5

wwwless site using url rewrite iis 8.5

我正在尝试删除 www.来自一个网站,所以我在我的基本页面上写了一个小代码。

    if (Request.Url.ToString().IndexOf("www.example.com") > -1)
    {
        Response.RedirectPermanent(Request.Url.ToString().Replace("www.example.com", "example.com"), true);
    }
    else
    {
        base.OnLoad(e);
    }

这有效,但在 SSL 页面上,浏览器给出证书错误,因为我的证书已签署到 example.com

所以我想我需要在 IIS 8.5 中使用 URL 重写模块 并尝试了这个。

<rewrite>
    <rules>
        <rule name="remove www." stopProcessing="true">
            <match url="^(.*)$" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="(www\.)(.*)$" />
            </conditions>
            <action type="Redirect" url="{C:2}" appendQueryString="false" redirectType="Permanent" />
        </rule>
    </rules>
</rewrite>

这失败了,因为它将 www.example.com 重定向到 www.example.com/example.com

请帮我写一个正确重定向的重写器。谢谢。

编辑:

我成功重定向了 http 页面,但 https 重定向仍然失败 ERR_CONNECTION_RESET。我写了 2 条规则

            <rule name="remove www. (https)" stopProcessing="true">
                <match url="^(.*)" ignoreCase="true" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    <add input="{HTTPS}" pattern="^ON" />
                    <add input="{HTTP_HOST}" pattern="^www\.example\.com" />
                </conditions>
                <action type="Redirect" url="https://example.com/{R:1}" redirectType="Permanent" />
            </rule>

            <rule name="remove www. (http)" stopProcessing="true">
                <match url="^(.*)" ignoreCase="true" />
                <conditions>
                    <add input="{HTTP_HOST}" pattern="^www\.example\.com" />
                </conditions>
                <action type="Redirect" url="http://example.com/{R:1}" redirectType="Permanent" />
            </rule>

我完全迷失在这里。为什么第一个规则不起作用但第二个规则起作用?

第二次编辑:

实际上,这些规则对于 http 和 https 有效ERR_CONNECTION_RESET 发生是因为 https 绑定丢失。但我仍然收到证书错误。

我接缝,浏览器不检查重定向并停止执行页面,以防出现证书错误。

将 www 转换为非 www

<rewrite>
  <rules>
    <rule name="Canonical" stopProcessing="true">
      <match url=".*" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="^www[.](.+)" />
      </conditions>
      <action type="Redirect" url="http://{C:1}/{R:0}"
          redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>

将非 www 转换为 www *确保更新域后缀

<rewrite>
  <rules>
    <rule name="Canonical" stopProcessing="true">
      <match url=".*" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="^([a-z]+[.]com)$" />
      </conditions>
      <action type="Redirect" url="http://www.{C:0}/{R:0}"
          redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>