Home > Backend Development > PHP Tutorial > PHP implementation of 301 permanent redirection and 302 temporary redirection method_PHP tutorial

PHP implementation of 301 permanent redirection and 302 temporary redirection method_PHP tutorial

WBOY
Release: 2016-07-13 10:49:43
Original
1298 people have browsed it

In the server, 301 and 302, for search engines, one is to permanently jump to a new address, and the other is to tell you that you have temporarily reached a new address. So how do we implement 301 permanent redirection and 302 temporary redirection in PHP? As for orientation, let's take a look at the implementation procedure of the method.


The principle of implementing redirection is very simple, that is, the web server returns an HTTP header to the visitor. The function of PHP sending HTTP header is implemented by the header() function. These status codes 301, 302, 404 are agreed upon in the HTTP protocol, so there is no need to ask "Why is it 301 instead of 3001?" Enough talk, let’s get back to the topic.

PHP 301 redirect:

The code is as follows Copy code
 代码如下 复制代码

header('HTTP/1.1 301 Moved Permanently'); 

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

exit();

header('HTTP/1.1 301 Moved Permanently');

Header( "Location: http://www.hzhuti.com/" );
 代码如下 复制代码

    //301永久重定向

$http_protocol = $_SERVER['SERVER_PROTOCOL']; //http协议版本

//如果是其他协议,则默认为HTTP/1.0
if ( 'HTTP/1.1' != $http_protocol && 'HTTP/1.0' != $http_protocol )
$http_protocol = 'HTTP/1.0';

//响应301状态码
header("$http_protocol 301 Moved Permanently");

//指定重定向的URL
$new_url = 'http://www.bKjia.c0m/';
header("Location:$new_url");
?>

exit();

 代码如下 复制代码

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

exit();

or

The code is as follows Copy code
代码如下 复制代码

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

exit();

//301 permanent redirect

$http_protocol = $_SERVER['SERVER_PROTOCOL']; //http protocol version

//If it is other protocols, the default is HTTP/1.0

If ( 'HTTP/1.1' != $http_protocol && 'HTTP/1.0' != $http_protocol )

$http_protocol = 'HTTP/1.0';
代码如下 复制代码

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/.jpg

<🎜> //Response to 301 status code<🎜> header("$http_protocol 301 Moved Permanently");<🎜> <🎜> //Specify the redirect URL<🎜> $new_url = 'http://www.bKjia.c0m/';<🎜> header("Location:$new_url");<🎜> ?> PHP 302 redirect:
The code is as follows Copy code
header("Location: http://www.hzhuti.com/"); exit();
By the way, the PHP 404 error is also attached:
The code is as follows Copy code
header("HTTP/1.1 404 Not Found"); exit();
That’s all about PHP 301 and 302 redirection. Here’s how to do it with Apache 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 (.*).gif$ http://www.bKjia.c0m/images/$1.jpg


2. Use mod_rewrite to rewrite the URL

APACHE

Options +FollowSymLinks RewriteEngine on
The code is as follows
 代码如下 复制代码

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

Copy code

RewriteCond %{HTTP_HOST} ^bKjia.c0m RewriteRule ^(.*)$ http://www.bKjia.c0m/$1 [R=permanent,L]

I won’t introduce anyone here about apache htaccess. The method of rewriting URLs with mod_rewrite is almost exactly the same. http://www.bkjia.com/PHPjc/632708.html
www.bkjia.comtrue
http: //www.bkjia.com/PHPjc/632708.htmlTechArticleIn the server, 301 and 302, for search engines, one is to permanently jump to a new address, and the other is to tell You have temporarily reached a new address, so how do we implement 301 permanent reset in php...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template