2017-02-05 259 views
0
<rule name="rd" stopProcessing="true"> 
     <match url="(.*)" /> 
     <conditions> 
     <add input="{HTTP_HOST}" pattern="^test\.com$" negate="true" /> 
     <add input="{HTTP_HOST}" pattern="^www.\test\.net$" negate="true" /> 
     </conditions> 
     <action type="Redirect" url="https://test.com/{R:0}" redirectType="Permanent" /> 
    </rule> 

此代碼的工作WWW:重定向HTTPS WWW到https非在IIS

http://www.test.com => https://test.com 

http://www.test.net=> https://test.com 

但不工作時,網址爲:

https://www.test.com => https://test.com 

https://www.test.net => https://test.com 

有什麼問題?

感謝FOT幫助

+0

如果你的條件之一是'',它爲什麼會起作用? – haim770

+0

當url已經使用HTTPS時,整個規則根本就不適用 – haim770

+0

我寫錯了,編輯過的帖子 – Ali

回答

0

我知道你說你想這樣做在IIS,但如果你能在應用程序中編輯Global.asax文件,你可以這樣來做。

protected void Application_BeginRequest(object sender, EventArgs e) 
{ 
     #if !DEBUG 
     string HTTPhost = Request.ServerVariables["HTTP_HOST"]; 

     string domainName = "test"; 

     //assuming that the app will only be reached via .COM or .NET 
     string topLevel = HTTPhost.Split('.').LastOrDefault(); 

     //compare to make sure it only matches the root name with no trailing subdomain 
     //or to redirect to secure url if unsecured 
     if ((!HTTPhost.Split('.').FirstOrDefault().Equals(domainName, StringComparison.InvariantCultureIgnoreCase)) 
     || (!HttpContext.Current.Request.IsSecureConnection)) 
     { 
      Response.RedirectPermanent(
       "https://" 
       + domainName 
       + '.' 
       + topLevel 
       + HttpContext.Current.Request.RawUrl); 
      //RawUrl means any url information after the domain 
     } 
     #endif 
} 
相關問題