301 redirect Implementation methods, and the role of Web site optimization _ Site Application

Source: Internet
Author: User
Tags comments configuration settings php code servervariables

What is a 301 redirect?

301 redirect is one of the status codes in the header information (header) in the HTTP data stream returned by the server when a user or search engine sends a browse request to the Web server, indicating that this page is permanently transferred to another address. Other common status code also includes, 200 means all normal, 404 Web pages can not find, 302 temporary turn, and so on.

Why do you turn with 301?

Web site steering methods include: 301-turn, 302-turn, JavaScript-turn, php/asp/cgi-turn, meta-refresh web meta refresh and so on. The 302 steering may have URL normalization issues. Other methods are commonly used cheating, of course, not to say that can not be properly used, the method itself is not wrong, but the cheater used more, the search engine for these suspicious turn are very sensitive. Why take the risk?

About 301 turn and website Normalization problem, everybody can go to read the article that Zac writes. In this paper, we only discuss how to set 301 redirection problem in ASP environment, other languages can be referenced.

We know that the homepage of the website generally has the following kinds:

Http://jb51.net
Http://www.jb51.net
Http://jb51.net/index.asp
Http://www.jb51.net/index.asp
......

Naturally, these Web site forms are all pointed to a website, if the search engines are enabled in these forms, then, the weight of the Web page, the PR will be dispersed to these several sites, although, they are a site, and this is not the result we want. Then, how to the above home form are directed to their desired web site, such as http://www.jb51.net, so that the weight of these decentralized web pages, PR are focused on the www.jb51.net. Here you need to use 301 permanent redirects.

ASP language, we need to add the following code at the top of the default page, such as index.asp:

<%
Dim DM,SN
Dm=request.servervariables ("SERVER_NAME") ' Get domain name
' Here's 3 refers to the WWW length
If left (dm,3) <> "www" Then
Response.status= "Moved Permanently"
Response.AddHeader "Location", "Http://www.jb51.net"
Response.End
End If
%>

The code here is processed by Flymorn, and you can make appropriate changes to the code to suit the style you need, depending on the actual needs. This code automatically check not entered the WWW, 301 jump to www.jb51.net this site, so that the weight of the page is cumulative, to achieve the purpose of SEO optimization.

But there's a problem here, if there's a link to http://www.jb51.net/ Index.asp (such as in-site links), then the search engine will also give this URL form bonus points, although index.asp is the default home page, but we hope that all weights are given to http://www.jb51.net this form. Some people say that these two forms are not the same? Not the same. For example: Http://www.jb51.net PR is 0, and http://www.jb51.net/index.asp PR is 2. It can be seen from this point that SE is given a different weight to the form of index.asp URLs. Using Request.ServerVariables ("Script_name") or request ("url") The suffix obtained is returned index.asp regardless of whether it is entered with index.asp, resulting in an inability to judge or use 301 redirection, of course, which refers to only the ASP locale.

The current solution is to use JS to obtain the current URL window.location.href, and then the current URL to write cookies;asp read this cookie, so as to determine whether the URL contains index.asp, if included, use 301 redirect. Suggestion: Avoid the form of direct pointing to index.asp in any link, including chain and inner chain, especially inside chain. This is only a compromise approach. To solve this problem, I hope to discuss with you master. If you have other solutions, please leave your comments.

Use JS through cookies to the ASP to pass the URL code as follows:

Copy Code code as follows:

<script language= "JavaScript" >
var url=window.location.href.tolowercase (); Lowercase
Document.cookie = "Urlck" + "=" + Escape (URL);
</script>

<%
Dim SN
Sn=request.cookies ("Urlck") ' Get suffix
Response.Cookies ("urlck") = "' Empty cookies

If Right (sn,11) = "Default.asp" Then
Response.status= "Moved Permanently"
Response.AddHeader "Location", "Http://www.jb51.net"
Response.End
End If
%>

But unfortunately, if the form of these URLs is not actually human input, but search engine access results, the search engine is to ignore JS, that is, JS in the assigned value of the cookie can not be recognized by SE, resulting in also can not use 301 jump. The problem remains to be solved.

301 redirects in other languages:

1, Unix/linux+apache host, in the. htaccess file, add 301 steering instructions, using mod_rewrite technology, such as: PHP code
Copy Code code as follows:


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


2, Window+iis host, the server software in the system administrator configuration settings, methods:

Open Internet Information Services Manager, right-click on the page or directory you want to redirect, select Redirect to URL, enter the address of the destination page in the dialog box, and remember to select the "Permanent redirect for resources"-> "apply."

3, with php/asp. NET implementation 301 Turn (HTML can not do 301 turn), the following methods:

1) with PHP implementation

PHP code
Copy Code code as follows:

? Header ("http/1.1 moved Permanently");
Header ("location:http://jb51.net");? >

2) realize with ASP.net

Asp/visual Basic code
Copy Code code as follows:

<script runat= "Server" >
private void Page_Load (object sender, System.EventArgs e)
{
Response.Status = "Moved Permanently";
Response.AddHeader ("Location", "http://jb51.net");
}
</script>

After these settings, the 301 turn should be configurable OK, if you want to check the correct, you can use the Internet to provide the server header check tool, such as Check server Headers tool-http Status codes Checker.

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

#1 Server response:http://jb51.net
HTTP Status code:http/1.1 Moved Permanently
Connection:close
Date:mon, Feb 2008 01:54:09 GMT
server: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 OK
Connection:close
Date:mon, Feb 2008 01:54:13 GMT
server: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 of the results returned above is "HTTP Status code:http/1.1 Moved Permanently", stating that this URL uses 301 permanent redirection features and is set successfully. If you have any comments on this article, you are welcome to leave a comment so that we can discuss it 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.