簡體   English   中英

在iis中將https www重定向到https non www

[英]redirect https www to https non www in iis

    <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>

此代碼在以下情況下有效:

 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

什么問題?!

謝謝幫助

我知道您已經說過要在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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM