【问题标题】:URL rewrite to redirect all URL variants to https://www.example.comURL 重写以将所有 URL 变体重定向到 https://www.example.com
【发布时间】:2017-02-24 08:57:09
【问题描述】:

我有一个 URL,并且我已将 SSL 证书应用于 www 变体 https://www.example.com。我希望这个域的所有变体都指向https://www.example.com

例如,以下域应重定向到https://www.example.com

网站托管在运行 IIS7.5 的 Windows 2008 服务器上,我使用 URL 重写在 web.config 文件中创建了一些规​​则。但是,以下域不会重定向:

以下是我目前的规则:

    <!-- Redirect http non www to https www -->
    <rule name="Redirect http://example.com to www" patternSyntax="Wildcard" stopProcessing="true">
      <match url="*" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="example.com" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
    </rule>

    <!-- Redirect http to https -->
    <rule name="Redirect http to https" enabled="true">
        <match url="(.*)" ignoreCase="false" />
        <conditions>
            <add input="{HTTPS}" pattern="off" />
        </conditions>
        <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
    </rule>

如果能通过 URL 重写来实现此功能,我将不胜感激。理想情况下,我更愿意用一条规则替换上述内容。

【问题讨论】:

    标签: regex https url-rewriting iis-7.5 windows-server-2008


    【解决方案1】:

    你最好使用两条规则,第一条将所有非 https 重定向到不带stopProcessing=true 的 https,第二条将所有非 www 重定向到 www,就像这样。

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

    另外,你不需要appendQueryString=true{R:1} 附加查询字符串。 stopProcessing=true 也不是必需的,特别是如果您没有遵循任何其他规则。但大多数情况下,如果您确实遵循规则,您希望它们在 URL 重定向到 https 并重新格式化为 www 后执行。

    【讨论】:

      【解决方案2】:

      让我们按照下面的逻辑,这是使用“正则表达式”。

      匹配任何传入的 URL,然后匹配任何以下条件。

      1. 如果主机开始匹配example.com然后重定向
      2. 如果使用端口80,则重定向。

      这将包括将 http 重定向到 https 以及将任何 example.com 重定向到 www.example.com

      <rule name="Do It All" stopProcessing="true">
        <match url="(.*)" />
        <conditions logicalGrouping="MatchAny" trackAllCaptures="false">
          <add input="{HTTP_HOST}" pattern="^example\.com$" />
          <add input="{SERVER_PORT}" pattern="^80$" />
        </conditions>
        <action type="Redirect" url="https://www.example.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
      </rule>
      

      【讨论】:

      • 感谢亨利。它肯定有所改善。在清除 cookie 和历史记录的缓存后,我在 Firefox 中对其进行了测试。以前,当我输入 example.com 时,它会立即重定向到 example.com,它标记了安全警告。现在它重定向到example.com,这就是我想要的。但是问题仍然存在,如果我输入example.com,它根本不会重定向并且会出现安全警告。
      • 如果有帮助,SSL 证书仅适用于 www.example.com 而不是 example.com。我想避免额外的费用,希望重定向可以解决问题。
      • 您是在全局级别还是在 Web.config 中应用此功能?尝试在它到达您的网站之前将其移动到全球级别。单击 IIS 中的服务器名称并将其应用到那里,它将进入您的 applicationhost.config。
      • 证书是在“服务器证书”应用程序中的服务器级别添加的。在站点级别,我为 https 创建了一个绑定,然后选择了证书。
      猜你喜欢
      • 2015-11-18
      • 2017-01-14
      • 1970-01-01
      • 2015-09-25
      • 1970-01-01
      • 2014-11-29
      • 2017-10-26
      • 2017-04-20
      • 2014-01-22
      相关资源
      最近更新 更多