ASP.net MVC site : Redirect all “non WWW” request to WWW

后端 未结 4 851
面向向阳花
面向向阳花 2021-02-04 18:26

Recently I migrated an ASP.net site to ASP.net MVC site. Earlier there were two host headers one mydomain.com and another is www.mydomain.com. My SEO says you should use only on

4条回答
  •  终归单人心
    2021-02-04 19:02

    You could use config or Url Rewriter in IIS, but the best method I've found is just to put some code in Application_BeginRequest() in your global.asax.cs like this:

    var HOST = "www.mydomain.com";
    
    if ( !Request.ServerVariables[ "HTTP_HOST" ].Equals(
      HOST,
      StringComparison.InvariantCultureIgnoreCase )
    )
    {
      Response.RedirectPermanent(
        ( HttpContext.Current.Request.IsSecureConnection ? "https://" : "http://" )
        + HOST
        + HttpContext.Current.Request.RawUrl );
    }
    

    Because you're doing this in code, you can have whatever logic you need on a per-request basis.

提交回复
热议问题