PHP Implementation 301 Permanent redirection and 302 temporary redirection method _php Tutorial

Source: Internet
Author: User
In the server 301 and 302 for the search engine is a permanent jump new address, one is to tell you temporarily to a new address, then we in PHP How to implement the 301 permanent Redirect and 302 temporary redirect, let's look at the method implementation program.


The principle of implementing redirection is simple, that is, the Web server returns an HTTP header to the visitor, and PHP sends the HTTP header, which is implemented by the header () function. 301, 302, 404 These Status codes are agreed in the HTTP protocol, so don't break the casserole and ask "why 301 instead of 3001". It's too much to get back to the chase.

PHP 301 Redirection:

The code is as follows Copy Code

Header (' http/1.1 301 Moved permanently ');

Header ("location:http://www.hzhuti.com/");

Exit ();

Or

The code is as follows Copy Code

301 Permanent Redirection

$http _protocol = $_server[' Server_protocol '); HTTP protocol version

If it is a different protocol, the default is http/1.0
if (' http/1.1 '! = $http _protocol && ' http/1.0 '! = $http _protocol)
$http _protocol = ' http/1.0 ';

Response 301 Status Code
Header ("$http _protocol 301 Moved permanently");

Specifies the URL of the redirect
$new _url = ' http://www.bKjia.c0m/';
Header ("Location: $new _url");
?>

PHP 302 Redirection:

The code is as follows Copy Code

Header ("Location:http://www. Hzhuti. com/");

Exit ();

Go to the PHP 404 error also included:

The code is as follows Copy Code

Header ("http/1.1 404 Not Found");

Exit ();

Here's what about PHP 301 and 302 redirects, and here's the Apache approach.

Cases:

Apache

The code is as follows Copy Code

Redirect 301/old/old.htm http://www.bKjia.c0m/new.htm
Redirect Permanent/one Http://bKjia.c0m/two
Redirectmatch 301 (. *). gif$ http://www.bkjia.c0m/images/$1.jpg


2. Use mod_rewrite to rewrite URL methods

Apache

The code is as follows Copy Code

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

About Apache Htaccess There's no reference here. The way to rewrite URLs with Mod_rewrite is almost exactly the same OH.

http://www.bkjia.com/PHPjc/632708.html www.bkjia.com true http://www.bkjia.com/PHPjc/632708.html techarticle in the server 301 and 302 for the search engine is a permanent jump new address, one is to tell you temporarily to a new address, then we in PHP how to achieve 301 permanent weight ...

  • 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.