ASP. NET 301 redirection Method

Source: Internet
Author: User

For Baidu and other search engines"WWW"Identify the domain name with a prefix:Www.abc.comAndAbc.comIt is identified as two different domain names. The consequence of doing so is to spread the attention of the website, which is not conducive to the promotion and promotion of the website. 

 

Only throughResponse. RedirectMethod to redirect the connection. Although the connection can be redirected, the problem of scattered search engine identification cannot be solved. This problem can be solved through 301 redirection.ASP. NETYou can use the following methods to handle this problem:

 

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 // Avoid replacing the domain name in the following parameters
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}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.