Php implements 301 redirection jump code

Source: Internet
Author: User
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:

  1. $ The_host = $ _ SERVER ['http _ host'];
  2. $ Request_uri = isset ($ _ SERVER ['request _ URI '])? $ _ SERVER ['request _ URI ']: '';
  3. If ($ the_host = 'www .phpfensi.com ')
  4. {
  5. Header ('http/1.1 301 Moved Permanently ');
  6. Header ('Location: http://phpfensi.com '. $ request_uri );//
  7. }
  8. ?>

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:

  1. If ($ HTTP_HOST = "www.phpfensi.com") or ($ HTTP_HOST = "phpfensi.com "))
  2. {
  3. Header ("HTTP/1.1 301 Moved Permanently ");
  4. Header ("Location:/index. php ");
  5. }
  6. Elseif ($ HTTP_HOST = "www.phpfensi.com ")
  7. {
  8. Header ("HTTP/1.1 301 Moved Permanently ");
  9. Header ("Location: http://phpfensi.com ");
  10. }
  11. Else
  12. {
  13. Header ("Location:/404.htm ");
  14. }

The code is as follows:

  1. // Define the encoding
  2. Header ('content-Type: text/html; charset = utf-8 ');
  3. // Atom
  4. Header ('content-type: application/atom + XML ');
  5. // CSS
  6. Header ('content-type: text/css ');
  7. // Javascript
  8. Header ('content-type: text/javascript ');
  9. // JPEG Image
  10. Header ('content-type: image/jpeg ');
  11. // JSON
  12. Header ('content-type: application/json ');
  13. // PDF
  14. Header ('content-type: application/pdf ');
  15. // RSS
  16. Header ('content-Type: application/rss + xml; charset = ISO-8859-1 ');
  17. // Text (Plain)
  18. Header ('content-type: text/plain ');
  19. // XML
  20. Header ('content-type: text/XML ');
  21. // OK
  22. Header ('http/1.1 200 OK ');
  23. // Set a 404 header:
  24. Header ('http/1.1 404 Not Found ');
  25. // Set the address to be permanently redirected
  26. Header ('http/1.1 301 Moved Permanently ');
  27. // Go to a new address
  28. Header ('Location: http://www.example.org /');
  29. // File delay redirection:
  30. Header ('refresh: 10; url = http://www.example.org /');
  31. Print 'You will be redirected in 10 seconds ';
  32. // Of course, you can also use html syntax to implement
  33. //  
  34. // Override X-Powered-By: PHP:
  35. Header ('x-Powered-By: PHP/4.4.0 ');
  36. Header ('x-Powered-By: Brain/0.6b ');
  37. // Document language
  38. Header ('content-language: en ');
  39. // Tell the browser the last modification time
  40. $ Time = time ()-60; // or filemtime ($ fn), etc
  41. Header ('last-Modified: '. gmdate ('d, d m y h: I: S', $ time). 'gmt ');
  42. // Tell the browser that the document content has not changed
  43. Header ('http/1.1 304 Not modified ');
  44. // Set the content length
  45. Header ('content-Length: 1234 ');
  46. // Set it to a download type
  47. Header ('content-Type: application/octet-stream ');
  48. Header ('content-Disposition: attachment; filename = "example.zip "');
  49. Header ('content-Transfer-Encoding: binary ');
  50. // Load the file to send:
  51. Readfile('example.zip ');
  52. // Disable caching for the current document
  53. Header ('cache-Control: no-Cache, no-store, max-age = 0, must-revalidate ');
  54. Header ('expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  55. Header ('pragma: no-cache ');
  56. // Set the content type:
  57. Header ('content-Type: text/html; charset = ISO-8859-1 ');
  58. Header ('content-Type: text/html; charset = utf-8 ');
  59. Header ('content-Type: text/plain '); // plain text format
  60. Header ('content-Type: image/jpeg '); // JPG ***
  61. Header ('content-Type: application/zip'); // zip file
  62. Header ('content-Type: application/pdf '); // pdf file
  63. Header ('content-Type: audio/mpeg '); // audio File
  64. Header ('content-Type: application/x-shockw ** e-Flash'); // flash animation
  65. // Display the login dialog box
  66. Header ('http/1.1 401 unauthorized ');
  67. Header ('www-Authenticate: Basic realm = "Top Secret "');
  68. Print 'text that will be displayed if the user hits cancel or ';
  69. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.