Php implements 301 permanent redirection and 302 temporary redirection Methods

Source: Internet
Author: User

In the server, 301 and 302 are a permanent jump address for the search engine, and one is to tell you that a new address has arrived, so how can we implement 301 permanent redirection and 302 temporary redirection in php? Let's take a look at the implementation program of the method.


The principle of implementing redirection is very simple, that is, the Web server returns an HTTP header to the login visitor. The HTTP header sent by PHP is implemented by the header () function. 301,302,404 these status codes are agreed in the HTTP protocol, so you don't need to break the sandpot and ask "Why is 301 instead of 3001 ". Go back to the question.

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

<? Php
// 301 permanent redirection

$ Http_protocol = $ _ SERVER ['server _ Protocol']; // http PROTOCOL version

// If it is another protocol, the default value is HTTP/1.0.
If ('HTTP/1.1 '! = $ Http_protocol & 'HTTP/123 '! = $ Http_protocol)
$ Http_protocol = 'HTTP/1.0 ';

// Response 301 status code
Header ("$ http_protocol 301 Moved Permanently ");

// Specify the redirected URL
$ New_url = 'HTTP: // www. bKjia. c0m /';
Header ("Location: $ new_url ");
?>

PHP 302 redirection:

The Code is as follows: Copy code

Header ("Location: http: // www.Hzhui. Com /");

Exit ();

The PHP 404 error is also included with the following code:

The Code is as follows: Copy code

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

Exit ();

Here we will talk about php 301 and 302 redirection. The following is an example of apache practice.

Example:

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 (. * mirror.gif $ http://www.bKjia. c0m/images/FIG


2. Use mod_rewrite to rewrite the URL

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]

For apache htaccess, the method of rewriting the URL is almost the same as that of mod_rewrite.

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.