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.