ASP. NET implements 301 redirection method

  • 2021-10-24 19:25:12
  • OfStack


<span style="font-family:' Song Style ';font-size:10.5pt;"></span> 

Baidu and other search engines identify domain names prefixed with "www": that is, search engines will identify www. abc. com and abc. com as two different domain names, which will distract the attention of websites and is not conducive to the publicity and promotion of websites.

Only through Response. Redirect method to redirect the connection, although the connection can be redirected, but can not solve the search engine identification dispersion problem; This problem can be solved by 301 redirection, specifically in ASP. NET by the following methods:


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 the domain name in the following parameters  
         context.Response.AddHeader( 
           "Location",  
           url.AbsoluteUri.Replace( 
             string.Format("http://{0}", host),  
             string.Format("http://www.{0}", host) 
             ) 
           ); 
       } 

More examples of methods implemented by ASP. NET301:

Because IIS Settings 301 need to be configured in the server is very troublesome, so ME chose to implement in the program.
The implementation of the program has a disadvantage is that the execution efficiency is not as fast as in the IIS server.

Of course, what is said here is only suitable for dynamic websites. If they are all. html static files, they will drift across!

Ok, let's just go to the code:

Home page file index. aspx background code


// Determine whether it is www. At the beginning, 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 it is my independent site here, I wrote www directly. If it is a level 2 domain name, it needs to be repaired according to its needs.

Column page/content page code:

//If the end of url does not end with/symbol, the/symbol is also added 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(); 
}


Related articles: