In php, the 301 redirection implementation method is very simple. we only need to simply use the header to send the 301 status code and then use the header to jump. The effect is the same as that of apache, iis, and nginx.
In php, the 301 redirection implementation method is very simple. we only need to simply use the header to send the 301 status code and then use the header to jump. The effect is the same as that of apache, iis, and nginx.
I. This method is recommended because it transfers all the original URLs of www.phpfensi.com to the new address of phpfensi.com. the code is as follows:
-
- $ The_host = $ _ SERVER ['http _ host'];
- $ Request_uri = isset ($ _ SERVER ['request _ URI '])? $ _ SERVER ['request _ URI ']: '';
- If ($ the_host = 'www .phpfensi.com ')
- {
- Header ('http/1.1 301 Moved Permanently ');
- Header ('Location: http://phpfensi.com '. $ request_uri );//
- }
- ?>
2. the single-page multi-site Php301 redirection code, www.phpfensi.com and phpfensi.com are 301 to index. php, and www.phpfensi.com is 301 to phpfensi.com. Otherwise, the error page is displayed. the code is as follows:
- If ($ HTTP_HOST = "www.phpfensi.com") or ($ HTTP_HOST = "phpfensi.com "))
- {
- Header ("HTTP/1.1 301 Moved Permanently ");
- Header ("Location:/index. php ");
- }
- Elseif ($ HTTP_HOST = "www.phpfensi.com ")
- {
- Header ("HTTP/1.1 301 Moved Permanently ");
- Header ("Location: http://phpfensi.com ");
- }
- Else
- {
- Header ("Location:/404.htm ");
- }
The code is as follows:
- // Define the encoding
- Header ('content-Type: text/html; charset = utf-8 ');
- // Atom
- Header ('content-type: application/atom + XML ');
- // CSS
- Header ('content-type: text/css ');
- // Javascript
- Header ('content-type: text/javascript ');
- // JPEG Image
- Header ('content-type: image/jpeg ');
- // JSON
- Header ('content-type: application/json ');
- // PDF
- Header ('content-type: application/pdf ');
- // RSS
- Header ('content-Type: application/rss + xml; charset = ISO-8859-1 ');
- // Text (Plain)
- Header ('content-type: text/plain ');
- // XML
- Header ('content-type: text/XML ');
- // OK
- Header ('http/1.1 200 OK ');
- // Set a 404 header:
- Header ('http/1.1 404 Not Found ');
- // Set the address to be permanently redirected
- Header ('http/1.1 301 Moved Permanently ');
- // Go to a new address
- Header ('Location: http://www.example.org /');
- // File delay redirection:
- Header ('refresh: 10; url = http://www.example.org /');
- Print 'You will be redirected in 10 seconds ';
- // Of course, you can also use html syntax to implement
- //
- // Override X-Powered-By: PHP:
- Header ('x-Powered-By: PHP/4.4.0 ');
- Header ('x-Powered-By: Brain/0.6b ');
- // Document language
- Header ('content-language: en ');
- // Tell the browser the last modification time
- $ Time = time ()-60; // or filemtime ($ fn), etc
- Header ('last-Modified: '. gmdate ('d, d m y h: I: S', $ time). 'gmt ');
- // Tell the browser that the document content has not changed
- Header ('http/1.1 304 Not modified ');
- // Set the content length
- Header ('content-Length: 1234 ');
- // Set it to a download type
- Header ('content-Type: application/octet-stream ');
- Header ('content-Disposition: attachment; filename = "example.zip "');
- Header ('content-Transfer-Encoding: binary ');
- // Load the file to send:
- Readfile('example.zip ');
- // Disable caching for the current document
- Header ('cache-Control: no-Cache, no-store, max-age = 0, must-revalidate ');
- Header ('expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
- Header ('pragma: no-cache ');
- // Set the content type:
- Header ('content-Type: text/html; charset = ISO-8859-1 ');
- Header ('content-Type: text/html; charset = utf-8 ');
- Header ('content-Type: text/plain '); // plain text format
- Header ('content-Type: image/jpeg '); // JPG ***
- Header ('content-Type: application/zip'); // zip file
- Header ('content-Type: application/pdf '); // pdf file
- Header ('content-Type: audio/mpeg '); // audio File
- Header ('content-Type: application/x-shockw ** e-Flash'); // flash animation
- // Display the login dialog box
- Header ('http/1.1 401 unauthorized ');
- Header ('www-Authenticate: Basic realm = "Top Secret "');
- Print 'text that will be displayed if the user hits cancel or ';
- Print 'enters wrong login data ';
Pay attention to the following points to help solve some problems that beginners often encounter.
1. there must be no space between location and:. Otherwise, an error will occur.
2. there cannot be any output before using the header.
3. the PHP code after the header is executed.