12 code instances to implement the 301 Web page redirection method (including Web programming language and Web server) _ Related tips

Source: Internet
Author: User
Tags php redirect ruby on rails

Why you need to use 301 redirects:

1. Keep Search engine rankings : 301 Redirect is the most effective way, will not affect the search engine rankings of the page.

2. Keep visitors and traffic : If you link the page to an address that is accessible to a large number of methods, you will lose these users if you do not redirect it (puzzled) Original: If you move your popular page to which a lot of visit ORS have already linked, you may lose them if you don ' t used redirect method. This provides a great way to provide your visitors with the information they were looking for and prevent your from losing Your traffic.

Here's how to implement the 301 redirect in 11:

1. HTML redirect/meta refresh

Place the following HTML redirection code in the <meta http-equiv= "REFRESH" content= "0; Url=http://www.yourdomain.com ">
The code immediately redirects the visitor to another page, and you can change the value of 0 in the content to indicate a few seconds before redirecting. For example content= "3"; url=http://www.oschina.net/"represents three seconds before redirection.

2. PHP redirect

Copy Code code as follows:
?
Header ("http/1.1 moved Permanently");
Header ("location:http://www.new-url.com");
?>

3. ASP Redirect

Copy Code code as follows:

<%@ Language=vbscript%>
<%
Response.status= "Moved Permanently"
Response.AddHeader "Location", "http://www.new-url.com/"
%>

4. ASP. NET Redirect

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://www.new-url.com");
}
</script>

5. JSP Redirect

Copy Code code as follows:

<%
Response.setstatus (301);
Response.setheader ("Location", "http://www.new-url.com/");
Response.setheader ("Connection", "close");
%>

6. CGI PERL Redirect

Copy Code code as follows:

$q = new CGI;
Print $q->redirect ("http://www.new-url.com/");

7. Ruby on Rails Redirect

Copy Code code as follows:

def old_action
headers["Status"] = "Moved Permanently"
Redirect_to "http://www.new-url.com/"
End

8. ColdFusion Redirect

Copy Code code as follows:

<.cfheader statuscode= "statustext=" "Moved Permanently" >
<.cfheader name= "Location" value= "http://www.new-url.com" >

9. Javascript URL Redirect

Copy Code code as follows:

<script type= "Text/javascript" >
window.location.href= ' http://www.newdomain.com/';
</script>

IIS Redirect
Right-click the files and folders you want to redirect in Internet Services Manager and select "A redirection to a URL".
Then enter the destination URL, select "The exact URL entered above" and "A permanent redirection for this resource" then click the ' Apply ' button.

11. Use. htaccess for redirection

Create a. htaccess file (code below) to redirect access domain.com to www.domain.com, the file must be placed in the root directory of the Web site, which is the directory where the first page is placed.

Copy Code code as follows:
Options +followsymlinks
Rewriteengine on
Rewritecond%{http_host} ^domain.com [NC]
Rewriterule ^ (. *) $ http://www.domain.com/$1 [R=301,NC]

Note: The redirection of the. Htaccess method can only be used if it is enabled under Linux using the Apache mod_rewrite module.

The rewrite in 12.Nginx

Copy Code code as follows:

server {
server_name www.jb51.net jb51.net;
if ($host = ' jb51.net ') {
Rewrite ^/(. *) $ http://www.jb51.net/$1 permanent;
}

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.