301 Implementation of redirection and Its Effect on website Optimization

Source: Internet
Author: User
Tags servervariables website server microsoft iis

What is 301 redirection?

301 redirection is a status code in the header of the HTTP data stream returned by the server when a user or search engine sends a browser request to the website server, indicates that the webpage is permanently transferred to another address. Other common status codes include 200 indicating that everything is normal, 404 webpages cannot be found, and 302 temporarily switched.

Why 301 redirection?

Website redirection methods include 301 redirection, 302 redirection, JavaScript redirection, PHP/asp/cgi redirection, and Meta Refresh. 302 redirection may cause URL Normalization Problems. Other methods are commonly used cheating methods. Of course, they are not to say that they cannot be used locally. the method itself is not wrong, but many people are used by writers. search engines are sensitive to these suspicious turns. Why take risks.

For more information about the 301 redirection and URL standardization, read the information written by Zac.Article. This article only discusses how to set 301 redirection in an ASP environment. For other languages, see.

We know that the homepage of a website generally has the following types:


Naturally, the above URL forms all point to a website. If these forms are enabled in search engines, the webpage weights and PR will be distributed to these URLs, they are all a website, and this is not the result we want. So, how to direct the above form of home pages to their desired web site, such as http://www.jb51.net, so that these scattered web page weight, PR are concentrated on www.jb51.net. Here we need to use 301 for permanent redirection.

In ASP, we need to add the following to the default homepage, for example, index. aspCode:

Dim DM, Sn
Dm = request. servervariables ("SERVER_NAME") 'Get the Domain Name
'Here 3 refers to the length of WWW.
If left (DM, 3) <> "www" then
Response. Status = "301 moved permanently"
Response. addheader "location", "http://www.jb51.net"
Response. End
End if

The code here is processed by flymorn. You can also modify the code to suit your needs. In this Code, if no WWW is entered, 301 is redirected to the URL www.jb51.net, so that the weight of the webpage is accumulated to achieve Seo optimization.

But there is a problem here, if there is a link pointing to the http://www.jb51.net/index.asp (such as the intra-site Link), then the search engine will also give this URL form points, although index. ASP is the default home page, but we hope to give ownership to the form of http://www.jb51.net. Some people say that these two forms are not the same? Different. For example: the http://www.jb51.net PR is 0, and http://www.jb51.net/index.asppr2. From this point, we can see that Se gives different weights to the URLs with index. asp. Request. servervariables ("script_name") or request ("url") obtains the suffix no matter whether the input has an index or not. all ASP returns index. ASP, and thus cannot be judged, and 301 redirection cannot be used. Of course, this refers to only the ASP language environment.

The current solution is to use js to obtain the current website window. location. href, and then write the current URL into the cookie. asp reads the cookie to determine whether the URL contains the index. ASP. If included, 301 redirection is used. Suggestion: Avoid direct link to index. asp in any link, including external links and internal links, especially internal links. This is just a compromise. We hope to discuss this issue with you. If you have other solutions, please leave your comments.

The code for passing a URL to ASP through cookies in JS is as follows:

Copy code The Code is as follows: <script language = "JavaScript">
VaR url = Window. Location. href. tolowercase (); // lower case
Document. Cookie = "urlck" + "=" + escape (URL );

Dim Sn
Sn = request. Cookies ("urlck") 'Get the suffix
Response. Cookies ("urlck") = "" 'clear cookies

If right (Sn, 11) = "Default. asp" then
Response. Status = "301 moved permanently"
Response. addheader "location", "http://www.jb51.net"
Response. End
End if

However, unfortunately, if these URLs are not input manually, but are accessed by the search engine, the search engine ignores JS, that is, the cookie assigned in JS cannot be identified by Se, and thus 301 redirection cannot be used. This problem is to be solved.

Redirection 301 in other languages:

1. for Unix/Linux + Apache hosts, add 301 redirection commands to the. htaccess file and use mod_rewrite technology, such as PHP code.Copy codeThe Code is as follows:

Options + followsymlinks
Rewriteengine on
Rewritecond % {http_host} ^ www.jb51.net [Nc]
Rewriterule ^ (. *) $ http://jb51.net/#1 [L, r = 301]

2. on the window + IIS host, set it in the system administrator configuration of the server software:

Open Internet Information Service Manager, right-click the webpage or directory to be redirected, select "redirect to URL", and enter the address of the target page in the dialog box, remember to select "permanent redirection of resources"-> "application ".

3. Use PHP/ASP. NET to implement 301 redirection (HTML cannot perform 301 redirection). The method is as follows:

1) implemented using PHP

PHP codeCopy codeThe Code is as follows: <? Header ("HTTP/1.1 301 moved permanently ");
Header ("Location: http://jb51.net");?>

2) implemented using ASP. NET

ASP/Visual Basic CodeCopy codeThe Code is as follows: <SCRIPT runat = "server">
Private void page_load (Object sender, system. eventargs E)
Response. Status = "301 moved permanently ";
Response. addheader ("location", "http://jb51.net ");

After these settings, the 301 redirection configuration should be OK. If you want to check whether it is correct, you can use the server header check tool provided on the Internet, for example, check server headers tool-HTTP status codes checker.

With the above HTTP status code detection tool, you can do a simple check, such as checking the http://jb51.net, the returned results are as follows:

#1 Server Response: http://jb51.net
HTTP status code: HTTP/1.1 301 moved permanently
Connection: Close
Date: Mon, 25 Feb 2008 01:54:09 GMT
Server: Microsoft-Microsoft IIS/6.0
X-powered-by: ASP. NET
Location: http://www.jb51.net
Content-Length: 0
Content-Type: text/html
Set-COOKIE: aspsessionidacbcsbdr = fcjelllalfnpocmbnmolmjhn; Path =/
Cache-control: Private
Redirect target: http://www.jb51.net

#2 Server Response: http://www.jb51.net
HTTP status code: HTTP/1.1 200 OK
Connection: Close
Date: Mon, 25 Feb 2008 01:54:13 GMT
Server: Microsoft-Microsoft IIS/6.0
X-powered-by: ASP. NET
Content-Length: 29330
Content-Type: text/html
Set-COOKIE: aspsessionidacbcsbdr = gcjelllaecpbadkmadajoblc; Path =/
Cache-control: Private

Note that the second line "HTTP status code: HTTP/1.1 301 moved permanently" in the returned results indicates that the URL adopts the 301 permanent Redirect function and is set successfully. If you have any opinions on this article, you are welcome to leave your comments so that we can discuss them together.

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.