2016-11-30 61 views
1

我想重寫HTTP到我的IIS 8.5 Web服務器上特定子文件夾的HTTPS,但它不工作。我讀過無數其他解決方案和博客文章,但沒有我嘗試過的作品。只重寫子文件夾(HTTP到HTTPS)重定向到站點根目錄

http://domain.example.com/one/two/three/
應該重定向到...(相同的URL,但使用HTTPS)
https://domain.example.com/one/two/three/

而是被重定向到...(使用站點根HTTPS)
https://domain.example.com

加載。 ..(使用https所需的網址)
https://domain.example.com/one/two/three/
也會重定向到...(使用https的網站根目錄)
https://domain.example.com

它將從url中刪除子文件夾。

此文件夾還需要使用Windows身份驗證進行保護,我可以開始工作,但https重定向失敗,無論是否啓用身份驗證,所以我不認爲這是原因。

在IIS中,我選擇了所需的子文件夾(在上例中爲/ three /),並在那裏創建了重寫規則。

<rewrite> 
    <rules> 
     <clear /> 
     <rule name="HTTP to HTTPS redirect" enabled="true" stopProcessing="true"> 
      <match url="(.*)" /> 
      <conditions logicalGrouping="MatchAny"> 
       <add input="{HTTPS}" pattern="^OFF$" /> 
      </conditions> 
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="SeeOther" /> 
     </rule> 
    </rules> 
</rewrite> 

這當然適用於所需子文件夾中包含的任何文件和文件夾。 (/三相)

我想這和它重定向到明顯的正確的URL,但給出了「過多的重定向」錯誤:

<rule name="HTTP to HTTPS redirect" enabled="true" stopProcessing="true"> 
    <match url="(.*)" /> 
    <conditions logicalGrouping="MatchAll"> 
     <add input="{HTTPS}" pattern="^OFF$" /> 
    </conditions> 
    <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" redirectType="SeeOther" /> 
</rule> 

回答

0

你應該避免這樣做:

Within IIS I selected the desired subfolder (/three/ in the example above) and created the Rewrite rule there.

相反在應用程序根目錄下的Web.config中設置重寫規則。您可以通過它在match參數如下重定向特定文件夾到https:

<?xml version="1.0" encoding="UTF-8"?> 
<configuration> 
    <system.webServer> 
     <rewrite> 
      <rules> 
       <rule name="Redirect Subfolder" stopProcessing="true"> 
        <match url="^one/two/three/" /> 
        <conditions logicalGrouping="MatchAll"> 
         <add input="{HTTPS}" pattern="^OFF$" /> 
        </conditions> 
        <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" redirectType="Permanent" /> 
       </rule> 
      </rules> 
     </rewrite> 
    </system.webServer> 
</configuration> 

請注意,這是一個很小Web.config文件已經做了你在找什麼。如果您的應用程序已經在根文件夾中包含Web.config,那麼您將不得不將上述內容合併到您的解決方案中。

相關問題