ASP.NET處理301重定向方法



    關於百度等搜索引擎對於是否帶"www"前綴的域名的識別問題:即搜索引擎會將www.abc.comabc.com識別為不同的兩個域名,這樣做的后果就是分散了對網站的關注度,不利於網站的宣傳和推廣。 

 

    僅僅是通過Response.Redirect方法來重定向該連接,雖然可以將連接進行重定向,但是無法解決搜索引擎的識別分散問題的;此問題可通過301重定向來進行解決,具體在ASP.NET中可通過如下方法來處理:

 

 1          private  void CheckTopDomainName(HttpContext context)
 2         {
 3             Uri url = context.Request.Url;
 4              string host = url.Host.ToLower();
 5 
 6              int count = host.Split( ' . ').Length;
 7              bool doubleDomainName = host.EndsWith( " .com.cn ", StringComparison.CurrentCultureIgnoreCase) ||
 8                 host.EndsWith( " .net.cn ", StringComparison.CurrentCultureIgnoreCase) ||
 9                 host.EndsWith( " .gov.cn ", StringComparison.CurrentCultureIgnoreCase) ||
10                 host.EndsWith( " .org.cn ", StringComparison.CurrentCultureIgnoreCase);
11 
12              if (count ==  2 || (count ==  3 && doubleDomainName))
13             {
14                 context.Response.Status =  " 301 Moved Permanently ";
15                  //  避免替換掉后面的參數中的域名
16                  context.Response.AddHeader(
17                      " Location "
18                     url.AbsoluteUri.Replace(
19                          string.Format( " http://{0} ", host), 
20                          string.Format( " http://www.{0} ", host)
21                         )
22                     );
23             }

24         } 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM