www.bob.tw

高級文盲

IIS, Windows

利用URL Rewrite自動轉址至HTTPS及虛擬目錄

準備工作:

一、安裝URL Rewrite,可參考:拾人牙慧 – 強化 IIS 安全性 ( web.config )

二、準備空白 index.htm


1、web.config (設定http 轉至 https ) 內容:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
     <system.webServer>
         <httpProtocol>
             <customHeaders>
                 <remove name="X-Powered-By" />
                 <add name="Strict-Transport-Security" value="max-age=31536000" />
                 <add name="X-Content-Security-Policy" value="default-src 'self'" />
                 <add name="X-Content-Type-Options" value="nosniff" />
                 <add name="X-Frame-Options" value="SAMEORIGIN" />
                 <add name="X-XSS-Protection" value="1; mode=block" />
             </customHeaders>
         </httpProtocol>
        <rewrite>
             <rules>
                 <rule name="http rediect to https" stopProcessing="true">
                     <match url="(.*)" />
                     <conditions>
                         <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                     </conditions>
                     <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
                 </rule>
             </rules>
         </rewrite>
    </system.webServer>
</configuration>

2、Edit index.htm (設定自動轉至虛疑目錄,本檔案請置放於 web root),可修改 location.href 後的路徑。

<script type="text/javascript">
     location.href = '/test123/';
</script>

3、按本文所敘設定後,其動作是,http自動轉至https,並轉至/test123;

例如:

網址敲入: http://www.168.coffee

瀏覽器顯示的網址: https://www.168.coffee/test123

備註:範例下載