Collect various program code for page 301 redirection

Source: Internet
Author: User

We have summarized the implementation code of the current mainstream program 301 page Jump, including asp, asp.net jsp, php cgi perl, etc. Generally, as long as it is a server program, it can jump 301, other status codes can also be implemented.

2. Code redirection in ASP 301

The Code is as follows: Copy code

<% @ Language = VBScript %>
<%
Response. Status = "301 Moved Permanently"
Response. AddHeader "Location", "http: // www. Your Website address. cn/articles/301 /"
%>

3. 301 redirection code under ASP. Net

The Code is as follows: Copy code

<Script runat = "server">
Private void Page_Load (object sender, System. EventArgs e)
{
Response. Status = "301 Moved Permanently ";
Response. AddHeader ("Location", "http: // www. Your Website address. cn/articles/301/");
}
</Script>

4. PHP 301 redirection code

The Code is as follows: Copy code

Header ("HTTP/1.1 301 Moved Permanently ");
Header ("Location: http: // www. Your Website address. cn/articles/301 /");
Exit ();

5. The 301 redirection code in CGI Perl

The Code is as follows: Copy code
$ Q = new CGI;
Print $ q-> redirect ("http: // www. Your Website address-url, com /");

6. 301 redirection code under JSP

The Code is as follows: Copy code

<%
Response. setStatus (301 );
Response. setHeader ("Location", "http: // www. Your Website address. cn /");
Response. setHeader ("Connection", "close ");
%>

Jump code first

The website port is not used for the time being. Generally, port 80 is used. If some websites have pages with different ports, you need to debug the port code before using it.

The Code is as follows: Copy code

<? Php
$ Dest_host = "www. Old network address. com"; // declare a variable $ dest_host to store the URL to jump
$ Url = $ _ SERVER ['request _ URI ']; // declare a variable $ url to store the host address and url of the current website.
$ Http = (isset ($ _ SERVER ['https']) & $ _ SERVER ['https']! = 'Off ')? 'Https: // ': 'http: //'; // declare the variable $ http storage protocol
// $ Port = $ _ SERVER ['server _ port'] = '80 ′?" : '$ _ SERVER ['server _ port']; // website PORT
$ Url = $ http. $ dest_host. $ url; // merged address
Switch ($ _ SERVER ['HTTP _ host']) {
Case "www. Website address. cn": // If the obtained host is www. Website address. cn
Header ('HTTP/1.1 301 Moved Permanently '); // load 301
Header ("location:". $ url );
Break;
Case "website address. cn": // or the website address. cn
Header ('HTTP/1.1 301 Moved Permanently '); // load 301
Header ("location:". $ url );
Break;
}
?>

Related Article

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.