简体   繁体   中英

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>

this code work when for example:

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

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

but not working when urls is:

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

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

what's problem?!

thanks fot helping

I know you've said you want to do this in IIS, but if you can edit the Global.asax file in the application, you can do it this way.

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM