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 process of the method... 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 have to break the sandpot and ask "why is it 301 instead of 3001.

PHP 301 redirection, the code is as follows:

header('HTTP/1.1 301 Moved Permanently'); Header( "Location: http://www.phprm.com/" );  exit();

// Or the following code

 

PHP 302 redirection, the code is as follows:

header("Location: http://www.phprm.com/");  exit();

The PHP 404 error is included with the code below:

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:

The APACHE code is as follows:

Redirect 301 /old/old.htm http://www.phprm.com/new.htm Redirect permanent /one http://phprm.com/two RedirectMatch 301 (.*).gif$ http://www.phprm.com/images/$1.jpg

Use mod_rewrite to rewrite the URL

The APACHE code is as follows:

Options +FollowSymLinks RewriteEngine on RewriteCond %{HTTP_HOST} ^phprm.com RewriteRule ^(.*)$ http://www.phprm.com/$1 [R=permanent,L]

Apache htaccess is not introduced here. it is almost the same as mod_rewrite to rewrite the URL.


Permanent address:

Reprint at will ~ Please bring the tutorial URL ^

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.