2014-09-30 81 views
5

我試圖使用web.config URL重寫規則強制HTTP到HTTPS在Azure網站上。我想這encompases以下兩種情形的規則:在Azure上強制HTTPS並追加URL

HTTP://site.domain.com/sitefolder到HTTPS://site.domain.com/sitefolder

HTTP://網站.domain.com/sitefolder/default.aspx到HTTPS://site.domain.com/sitefolder/default.aspx

如果我關注this guide,我可以強制HTTP轉換爲HTTPS,但URL更改爲HTTPS:// site .domain.com沒有附加/ sitefolder或/sitefolder/default.aspx。

這就是我目前所掌握的。 HTTPS是被迫的,但不包含完整的URL:

<rule name="Redirect to HTTPS" stopProcessing="true"> 
    <match url="(.*)" /> 
    <conditions> 
    <add input="{HTTPS}" pattern="Off" /> 
    </conditions> 
    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" /> 
</rule> 

回答

5

這是簡單的規則,使正確的重定向到HTTPS在Azure上的網站:

<rule name="Redirect to https"> 
     <match url="(.*)"/> 
     <conditions> 
     <add input="{HTTPS}" pattern="Off"/> 
     <add input="{REQUEST_METHOD}" pattern="^get$|^head$" /> 
     </conditions> 
     <action type="Redirect" url="https://{HTTP_HOST}/{R:1}"/> 
    </rule> 

你可以在這裏進行測試:http://double.azurewebsites.net(取在鏈接中的HTTP註釋)我沒有任何其他規則在這裏定義。上面的規則正確地重定向到深層鏈接,即http://double.azurewebsites.net/Home/About

+1

謝謝,這工作正好。乾杯。 – MK8 2014-09-30 23:21:35

+0

@ MK8考慮到它僅適用於head和get(意思是其他請求不會重新路由到HTTPS,如POST) – Ron 2015-07-19 00:16:49

+0

很多博客文章都是這樣的,但這是唯一真正有效的規則。 – 2015-10-15 02:31:14