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( "Location: http://www.hzhuti.com/" );
exit(); |
代码如下 | 复制代码 |
header("Location: http://www.hzhuti.com/"); exit(); |
The code is as follows | Copy code |
代码如下 | 复制代码 |
header("HTTP/1.1 404 Not Found"); exit(); |
$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 |
The code is as follows | Copy code |
header("Location: http://www.hzhuti.com/"); exit(); |
The code is as follows | Copy code |
header("HTTP/1.1 404 Not Found"); exit(); |
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
The code is as follows
|
Copy code
|
||||
Options +FollowSymLinks |
www.bkjia.comtruehttp: //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...