【问题标题】:IIS Redirect www to non-www and http to https: is it possible to do it with only one Redirect?IIS 将 www 重定向到非 www 和 http 到 https:是否可以仅使用一个重定向来完成?
【发布时间】:2016-09-16 14:22:19
【问题描述】:

在创建两个 IIS URL 重写规则后,我需要避免双重重定向:

1) 将 www 重定向到非 www。

2) 将 HTTP 重定向到 HTTPS。

这是我的代码:

<rule name="Redirect to https" stopProcessing="true">
    <match url="(.*)" />
    <conditions>
        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
    </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
<rule name="CanonicalHostNameRule1" enabled="true">
    <match url="(.*)" />
    <conditions>
        <add input="{HTTP_HOST}" matchType="Pattern" pattern="^mydomain\.com$" ignoreCase="true" negate="true" />
    </conditions>
    <action type="Redirect" url="https://ABC/{R:1}" />
</rule>

(ABC 是 mydomain.com 名称,但我必须更改它才能发布问题)

问题是,如果我转到 www,它会进行两次重定向,一次从 www 到非 www,第二次从 http 到 https。

我也尝试过在两种条件下都只使用一个规则,但结果并不好。

有没有办法只做一次重定向?

【问题讨论】:

  • 您可以通过您的 DNS 配置将 www 重定向到非 www,仅供参考...我不认为这被视为重定向。

标签: redirect iis https seo


【解决方案1】:

这是我使用的最终配置:

         <rewrite>
            <rules>
                <rule name="Force non-WWW and SSL" enabled="true" stopProcessing="true">
                  <match url="(.*)" />
                  <conditions logicalGrouping="MatchAny">
                      <add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
                      <add input="{HTTPS}" pattern="off" />
                  </conditions>
                  <action type="Redirect" url="https://yourdomainname.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
                </rule>
            </rules>
        </rewrite>

只有一条规则会重定向到非 www 和 https url。

【讨论】:

  • 此规则不适用于xxxx 所有其他组合都有效:(
【解决方案2】:

您应该能够使用 HTTP 到 HTTPS,相反的情况会更棘手,具体取决于您的 IIS 设置。

     <rule name="HTTP to HTTPs and www to non-www " enabled="true" stopProcessing="true">
         <match url="(.*)" />
         <conditions logicalGrouping="MatchAny" trackAllCaptures="false">
             <add input="{SERVER_PORT}" pattern="^80$" />
             <add input="{HTTP_HOST}" pattern="^www\.example\.com$" />
         </conditions>
         <action type="Redirect" url="https://example.com/{R:1}" />
     </rule>

请记住,根据您在 IIS 中构建站点的方式(绑定、站点等),这一切都会影响您的重写规则的工作方式以及您要放置它们的位置。在应用级别或作为全局规则。

由于我不知道您的绑定或 IIS 是如何与其他站点构建的,因此不可能 100% 准确地为您编写 URL 重写规则。 所以你可能需要对上面的语法做一些实验。

【讨论】:

    猜你喜欢
    • 2019-12-13
    • 1970-01-01
    • 1970-01-01
    • 2018-09-25
    • 2018-09-26
    • 2017-11-24
    • 2017-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多