Redirect IIS将www重定向到非www,将http重定向到https:是否可以只使用一个重定向?

Redirect IIS将www重定向到非www,将http重定向到https:是否可以只使用一个重定向?,redirect,iis,https,seo,Redirect,Iis,Https,Seo,创建两个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" /

创建两个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

我也尝试了两种情况下只有一条规则,但结果并没有更好


是否有一种方法只能执行一次重定向?

您应该能够使用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重写规则。
因此,您可能需要对上述语法进行一些实验。

这是我使用的最终配置:

         <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的规则。

您可以通过DNS配置将www重定向到非www,仅供参考。。。我不认为这是重定向。此规则不适用于所有其他组合:(