ASP.NET處理301重新導向方法

來源:互聯網
上載者:User

    關於百度等搜尋引擎對於是否帶"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         } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.