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