2017-09-11 51 views
0

在試圖將http流量重定向到https時,我發現了兩條下面的規則,它們似乎在做同樣的事情,但是它們在兩個地方有很小的差異。我應該更喜歡那一個嗎?有什麼好處嗎? (性能,拐角情況等)IIS重定向到HTTPS,不同的規則同樣的結果

規則1:

<rule name="HTTP to HTTPS Redirect" enabled="true" stopProcessing="true"> 
    <match url="(.*)" /> 
    <conditions logicalGrouping="MatchAny"> 
     <add input="{SERVER_PORT_SECURE}" pattern="^0$" /> 
    </conditions> 
    <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" /> 
</rule> 

規則2:

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

差異是在輸入和重定向的URL,其中一個用途{R:1}和另一個REQUEST_URI。

預先感謝您

回答

3

規則都給出相同的結果。他們之間的表現沒有顯着差異。除了默認情況下,IIS在內核級別緩存這些規則。這意味着請求很可能會從HTTP內核模式驅動程序響應,而不會到達Web應用程序。所以這些規則的工作速度將與您無法衡量差異一樣快。然而,如果你喜歡做不必要的優化(比如我有時會做:$),請檢查以下規則。

<rule name="HTTP to HTTPS Redirect" patternSyntax="Wildcard" stopProcessing="true"> 
    <match url="*" /> 
    <conditions> 
     <add input="{SERVER_PORT_SECURE}" pattern="0" ignoreCase="false" /> 
    </conditions> 
    <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" /> 
</rule> 

這裏是不必要旨在改進與此規則;

  • 通配符匹配比正則表達式便宜。
  • 區分大小寫又名二進制比較(ignoreCase="false")比較便宜。
  • 尋找0對於{SERVER_PORT_SECURE}比尋找off要便宜{HTTPS}
相關問題