關於百度等搜尋引擎對於是否帶"www"首碼的網域名稱的識別問題:即搜尋引擎會將www.abc.com和abc.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 }