Asp.net implementation of 301 redirection method

Time:2020-1-9
<span style= "font-family:'Tahoma'; font-size:10.5pt;" ></span>

On Baidu and other search engines for the “www” prefix domain name identification problem: that is, search engines will identify www.abc.com and abc.com as two different domain names, the consequences of this is to disperse the attention of the website, is not conducive to the publicity and promotion of the website.

The connection can only be redirected by the response.redirect method. Although the connection can be redirected, it cannot solve the problem of decentralized identification of search engines. This problem can be solved by 301 redirection, which can be handled in asp.net as follows:

private void CheckTopDomainName(HttpContext context) 
     { 
       Uri url = context.Request.Url; 
       string host = url.Host.ToLower(); 
  
       int count = host.Split('.').Length; 
       bool doubleDomainName = host.EndsWith(".com.cn", StringComparison.CurrentCultureIgnoreCase) || 
         host.EndsWith(".net.cn", StringComparison.CurrentCultureIgnoreCase) || 
         host.EndsWith(".gov.cn", StringComparison.CurrentCultureIgnoreCase) || 
         host.EndsWith(".org.cn", StringComparison.CurrentCultureIgnoreCase); 
  
       if (count == 2 || (count == 3 && doubleDomainName)) 
       { 
         context.Response.Status = "301 Moved Permanently"; 
         //Avoid replacing domain names in subsequent parameters 
         context.Response.AddHeader( 
           "Location",  
           url.AbsoluteUri.Replace( 
             string.Format("http://{0}", host),  
             string.Format("http://www.{0}", host) 
             ) 
           ); 
       }

More examples of asp.net301 implementation methods:

Because the IIS setting 301 needs to be configured in the server, so me chose to implement it in the program.
One disadvantage of the program implementation is that the execution efficiency is not fast in IIS server.

Of course, what I’m talking about here is only suitable for dynamic websites. If it’s all. HTML static files, just float by!

OK, let’s go straight to the code:

Index.aspx background code of homepage file

//Judge whether it is the beginning of www. if not 301, adjust to www. domain name 
if (!System.Web.HttpContext.Current.Request.Url.ToString().StartsWith("http://www.")) 
{ 
   //301 redirect to / directory           
   HttpContext.Current.Response.StatusCode = 301; 
   HttpContext.Current.Response.Status = "301 Moved Permanently"; 
   HttpContext.Current.Response.AddHeader("Location", "http://www.qinquan.org/"); 
   HttpContext.Current.Response.End(); 
}

Because this is my independent site, so directly write www. If it’s a secondary domain name, you need to repair it according to your own needs.

Code of column page / content page:

//If the URL does not end with the / symbol, add the / symbol from 301 to the end.

if (!System.Web.HttpContext.Current.Request.RawUrl.EndsWith("/")) 
{ 
     //301 redirect to / directory 
     HttpContext.Current.Response.StatusCode = 301; 
     HttpContext.Current.Response.Status = "301 Moved Permanently"; 
     HttpContext.Current.Response.AddHeader("Location", System.Web.HttpContext.Current.Request.RawUrl + "/");        
     HttpContext.Current.Response.End(); 
}