2017-02-05 8 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> 

でHTTPS以外のWWWこのコードの動作時にへのHTTPS WWWをリダイレクトは、例えば、IIS

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

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

問題何?私はあなたがIISでこれを行うにしたいと述べましたが、あなたはアプリケーションにGlobal.asaxファイルを編集することができた場合、あなたはこのようにそれを行うことができます知っている

+0

条件の1つが「」の場合、なぜ動作しますか? – haim770

+0

URLが既にHTTPSを使用している場合、ルール全体が単純に適用されません – haim770

+0

私は間違いを書いた、編集された投稿 – Ali

答えて

0

を支援FOT

感謝。

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 
}