Select Page

現在的網站一定都要有https的協定才會是安全的,SEO的分數也才會高,在IIS內可以直接透過 URL rewrite 來將 http 連線都轉換成 https 連線,過程比 nginx 要麻煩一點,但有圖形化的介面設定,也是蠻容易上手的

打開 IIS Manager 找到 URL Rewrite

先假設你已經完成了 URL Rewrite 的安裝,則可以在介面中看到 URL Rewrite的設定

建立新的規則 (Add Rule)

在右邊的 Actions 選擇 Add Rule(s),並且選擇 Blank rule

建立 Inbound Rule

如下圖,下拉方塊中選擇 Match URL,並且在 Pattern 中填入 (.*) ,然後移到下方 Conditions 選擇 Add

並且在 Condition input 填入 {HTTPS} ,在 Pattern 中填入 ^OFF$ ,下拉選單中則是選擇 Matches the Pattern

設定 Action

輸入網址的條件設定完之後,要來設定符合的網址要如何處理,如何做動作,參考下圖,Action type 的下拉選單,選擇 Redirect

並且在 Redirect URL 中,填入 https://{HTTP_HOST}/{R:1} ,並且到 Redirect type 下拉選單中選擇 Permanent(301) 後即可以存檔 (Apply)

Web.Config 中的 rewrite

用 iis manager 設定完畢後,你就可以在 web.config 中看到剛剛所做的設定

        <rewrite>
            <rules>
                <clear />
                <rule name="http2https" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{HTTPS}" pattern="^OFF$" />
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
                </rule>
            </rules>
        </rewrite>

參考資料

https://aboutssl.org/iis-redirection-http-to-https/