2014-09-10 150 views
5

我需要重定向所有HTTPS請求的HTTP,例如,如果有人訪問https://www.example.com/another-page/http://www.example.com/another-page/IIS重寫規則在web.config中重定向HTTPS請求HTTP

我在我的web.config以下重寫規則現在,但它不能正常工作。它正在將https://www.example.com/another-page/重定向到https://www.example.com/,所以到站點的根目錄,而是我希望重定向保留在同一個URL中,並且只將http重寫爲http。

<rule name="Redirect to HTTP" stopProcessing="true"> 
    <match url="(.*)" /> 
    <conditions> 
     <add input="{R:1}" pattern="^onepage/(.*)$" negate="true" /> 
     <add input="{HTTPS}" pattern="^ON$" /> 
    </conditions> 
    <action type="Redirect" url="http://{HTTP_HOST}/{R:0}" redirectType="Permanent" /> 
</rule> 

任何有關更改上述規則的幫助,以便它只將https更改爲http,但保留完整的url訪問將不勝感激!

+1

? – cheesemacfly 2014-09-12 00:46:07

+0

非常相關,雖然這個問題在技術上不是一個笨蛋:http://stackoverflow.com/questions/9823010/how-to-force-https-using-a-web-config-file – 2015-08-04 20:30:20

回答

2

請將下面的代碼粘貼到web.config文件中。

<rule name="Redirect to http" stopProcessing="true"> 
    <match url="(.*)" /> 
    <conditions> 
     <add input="{HTTP}" pattern="off" ignoreCase="true" /> 
    </conditions> 
    <action type="Redirect" url="http://{HTTPS_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" /> 
</rule> 
+0

謝謝,但正如在問題,網站運行IIS不是Apache,所以沒有.htaccess文件。相反,我使用的是web.config文件和IIS重寫模塊。 – IntricatePixels 2014-09-26 20:14:52

+0

對不起。請使用編輯後的代碼。 – 2014-09-29 12:47:14

11

我建立你的規則,清理一點,它的工作;所以這不是真的應對新的。

建議:刪除onepage輸入條件只是爲了測試,因爲cheesmacfly在問題評論中提出。

此外,請嘗試將操作更改爲{R:1}而不是{R:0}。在這種情況下應該沒有關係,但我只是喜歡使用1和以上來匹配特定的捕獲組。 R:0表示整個匹配的字符串,它總是讓我感到困惑。

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

一種可能性是您的瀏覽器緩存了先前您的規則嘗試。當redirectType是永久的,並且您仍在開發或測試時,瀏覽器通常會緩存先前的規則。清除您的瀏覽器緩存,和/或刪除永久,和/或瀏覽隱身模式。完成測試後,將其更改爲永久性。請參閱數字2和3在此答案:https://stackoverflow.com/a/9204355/292060

+0

這是正確的答案,謝謝 – Rippo 2016-08-10 13:56:44