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