IIS 設定 HTTP 導向 HTTPS (IIS Redirect Http to Http with Url Rewrite)


  1. 說明
    1. Web.config
      1. Anti Header Injection
  2. 參考資料

說明如何藉由 IIS Url Rewrite 設定自動將 Http Request 導向 Https。

logo

說明

站台上的繫結必須同時綁有 80 及 443 Port:

使用 IIS 的 Url Rewrite 進行設定,如果找不到此圖示可能是因為 IIS 並未安裝 Url Rewrite

新增一筆空白規則

依序輸入下列資訊:

樣式 (.*)

輸入 {HTTPS} 模式 ^OFF$

重新導向 https://{HTTP_HOST}{REQUEST_URI}

Web.config

上述的 Url Rewrite GUI 操作,相當於應用程式的 Web.config 中的 system.webServer 加入 Rewrite Rules。

<system.webServer>
    <rewrite>
        <rules>
            <rule name="Http To Https" stopProcessing="true">
                <match url="(.*)" />
                <conditions>
                    <add input="{HTTPS}" pattern="^OFF$" />
                </conditions>
                <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

Anti Header Injection

<system.webServer>
    <rewrite>
        <rules>
            <rule name="Http To Https" stopProcessing="true">
                <match url="(.*)" />
                <conditions>
                        <add input="{HTTPS}" pattern="^OFF$" />
                        <add input="{HTTP_HOST}" pattern="^(blog|www).sdwh.dev$" />
                </conditions>
                <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" />
            </rule>
            <rule name="Check Host" stopProcessing="true">
                <match url="(.*)" />
                <conditions>
                        <add input="{HTTP_HOST}" pattern="^(blog|www).sdwh.dev$" negate="true" />
                </conditions>
                <action type="Redirect" url="https://info.sdwh.dev/Invalida-Host" appendQueryString="false" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

參考資料

Setting up an HTTP/HTTPS redirect in IIS