Route www link to non-www link in .net mvc

前端 未结 3 1079
太阳男子
太阳男子 2021-01-01 04:40

It seems with the built in friendly routing library in .NET MVC, it would allow us to do something like this.

In case it\'s not obvious what I want to with the built

3条回答
  •  无人及你
    2021-01-01 05:27

    You can use IIS 7 URL Rewriting module

    You can setup it from IIS or just place in your web.config the following under :

    www to non-www

    
      
        
          
          
            
          
          
        
      
    
    

    Alternatively you can make this redirection on global.asax.cs:

    protected void Application_BeginRequest(object sender, EventArgs ev)
    {
        if (Request.Url.Host.StartsWith("www", StringComparison.InvariantCultureIgnoreCase))
        {
            Response.Clear();
            Response.AddHeader("Location", 
                String.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Host.Substring(4), Request.Url.PathAndQuery)
                );
            Response.StatusCode = 301;
            Response.End();
        }
    }
    

    But remeber what @Sam said, look here for more info.


    non-www to www

    
      
        
          
          
            
          
          
        
      
    
    

    Make a regex pattern to match your host and use {C:0} 1, 2, ..., N to get the matching groups.

提交回复
热议问题