wwwless网站使用url重写iis 8.5

时间:2015-02-26 09:45:37

标签: c# asp.net iis

我正在尝试删除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 。我写了两条规则

            <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绑定丢失而发生。但我仍然得到证书错误。

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

1 个答案:

答案 0 :(得分:1)

将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>
相关问题