簡體   English   中英

IIS 將 www 重定向到非 www,將 http 重定向到 https:是否可以僅通過一個重定向來完成?

[英]IIS Redirect www to non-www and http to https: is it possible to do it with only one Redirect?

在創建兩個 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 名稱,但我必須更改它才能發布問題)

問題是,如果我 go 到 www,它會進行兩次重定向,一次從 www 到非 www,第二次從 http 到 https。

我也嘗試過只有一個規則同時滿足兩種條件,但結果並沒有更好。

有沒有辦法只做一個重定向?

這是我使用的最終配置:

         <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 的一項規則。

您應該能夠使用 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 重寫規則。 因此,您可能需要對上述語法進行一些實驗。

當然。 只需刪除這些規則:(無需修改)

        <rule name="Redirect to HTTPS without www" stopProcessing="true">
          <match url="(.*)" />
          <conditions logicalGrouping="MatchAll">
            <add input="{HTTPS}" pattern="^OFF$" />
            <add input="{HTTP_HOST}" pattern="^(www\.)?(.*)$" />
          </conditions>
          <action type="Redirect" url="https://{C:2}/{R:1}" redirectType="Permanent" />
        </rule>
        <rule name="Special case for HTTPS with www" stopProcessing="true">
          <match url="(.*)" />
          <conditions logicalGrouping="MatchAll">
            <add input="{HTTPS}" pattern="^ON$" />
            <add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
          </conditions>
          <action type="Redirect" url="https://{C:2}/{R:1}" redirectType="Permanent" />
        </rule>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM