2015-03-31 90 views
2

我的目標是在IIS/Azure的web.config文件中具有成功重定向以下客戶端請求示例的規則:IIS/Azure web.config文件將流量重定向到非www的簡單規則HTTPS

HTTPS重定向:

無www的& HTTPS重定向:

無WWW,HTTPS重定向&維持路徑:

這裏是我到目前爲止,僅是實現點1和2。如果直接訪客的土地上的子頁面,他們不會被重定向到非www和沒有重定向到HTTPS。

<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> 
    <rule name="Canonical Hostname" stopProcessing="false"> 
     <match url="(.*)" /> 
     <conditions logicalGrouping="MatchAll" trackAllCaptures="false"> 
     <add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" /> 
     </conditions> 
     <action type="Redirect" url="https://{C:2}{REQUEST_URI}" redirectType="Permanent" /> 
    </rule> 

我已經筋疲力盡多個搜索結果頁面,並沒有任何的運氣多StackOverflow的問題 - 他們都承諾能夠實現第三點(重定向到一個非www,HTTPS 頁和維持路徑)但他們沒有爲我工作。

感謝您花時間閱讀本文!

回答

2

我發現了這個問題,部分原因是我提出了我的問題(對不起,StackOverflowers!)。

問題在於規則的排序。我將以下(新)規則放在我的規則部分的頂部,按照以下順序排列,供將來遇到此問題的人員使用。現在,它重定向到HTTPS,使用no-www,並維護客戶端請求的路徑。

<rule name="Canonical Hostname" stopProcessing="false"> 
    <match url="(.*)" /> 
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false"> 
     <add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" /> 
    </conditions> 
    <action type="Redirect" url="http://{C:2}{REQUEST_URI}" redirectType="Permanent" /> 
    </rule> 
    <rule name="Force HTTPS" enabled="true"> 
    <match url="(.*)" ignoreCase="false" /> 
    <conditions> 
     <add input="{HTTPS}" pattern="off" /> 
    </conditions> 
    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" /> 
    </rule> 
相關問題