How to enable gzip compression in a PHP virtual host

Source: Internet
Author: User
Tags gz file
  1. Define (' Abspath ', DirName (__file__). ' /');

  2. $cache = true;//gzip compression switch

  3. $cachedir = ' gzip_cache/';//directory where GZ files are stored, created before use, and given writable permissions

  4. $gzip = strstr ($_server[' http_accept_encoding '), ' gzip ');

  5. $deflate = strstr ($_server[' http_accept_encoding '), ' deflate ');
  6. $encoding = $gzip? ' gzip ': ($deflate? ' Deflate ': ' None ');

  7. if (!isset ($_server[' query_string ')) exit ();

  8. $key =array_shift (Explode ('? ', $_server[' query_string '));

  9. $key =str_replace ('.. /', ', $key);
  10. $filename =abspath. $key;
  11. $symbol = ' ^ ';
  12. $rel _path=str_replace (Abspath, ", DirName ($filename));
  13. $namespace =str_replace ('/', $symbol, $rel _path);
  14. $cache _filename=abspath. $cachedir. $namespace. $symbol. basename ($filename). GZ ';//cache path
  15. $type = "content-type:text/html"; MIME Information
  16. $ext = Array_pop (Explode ('. ', $filename));//Get the file name extension

  17. Switch ($ext) {//update MIME information

  18. Case ' CSS ':
  19. $type = "Content-type:text/css";
  20. Break
  21. Case ' JS ':
  22. $type = "Content-type:text/javascript";
  23. Break
  24. Default
  25. Exit ();
  26. }

  27. if ($cache) {

  28. if (file_exists ($cache _filename)) {//If there is a GZ file
  29. $mtime = Filemtime ($cache _filename);
  30. $gmt _mtime = gmdate (' d, D M Y h:i:s ', $mtime). ' GMT ';
  31. if ((Isset ($_server[' http_if_modified_since ')) &&
  32. Array_shift (Explode ('; ', $_server[' http_if_modified_since ')) = = = $gmt _mtime)
  33. ){
  34. Returns 304 if the file is not changed
  35. Header ("http/1.1 304 not Modified");
  36. Header ("Expires:");
  37. Header ("Cache-control:");
  38. Header ("Pragma:");
  39. Header ($type);
  40. Header ("Tips:cache not Modified (Gzip)");
  41. Header (' content-length:0 ');

  42. }else{

  43. Read GZ file output

  44. $content = file_get_contents ($cache _filename);
  45. Header ("last-modified:".) $gmt _mtime);
  46. Header ("Expires:");
  47. Header ("Cache-control:");
  48. Header ("Pragma:");
  49. Header ($type);
  50. Header ("Tips:normal Respond (Gzip)");
  51. Header ("Content-encoding:gzip");
  52. Echo $content;
  53. }

  54. }else if (file_exists ($filename)) {//does not have a corresponding GZ file

  55. $mtime = Mktime ();

  56. $gmt _mtime = gmdate (' d, D M Y h:i:s ', $mtime). ' GMT ';
  57. $content = file_get_contents ($filename);
  58. $content = Gzencode ($content, 9, $gzip? force_gzip:force_deflate);//compressed content
  59. Header ("last-modified:".) $gmt _mtime);
  60. Header ("Expires:");
  61. Header ("Cache-control:");
  62. Header ("Pragma:");
  63. Header ($type);
  64. Header ("Tips:build gzip File (gzip)");
  65. Header ("content-encoding:".) $encoding);
  66. Header (' Content-length: '. strlen ($content));
  67. Echo $content;

  68. if ($fp = fopen ($cache _filename, ' W ')) {//write cache

  69. Fwrite ($fp, $content);
  70. Fclose ($FP);
  71. }

  72. }else{

  73. Header ("http/1.0 404 Not Found");
  74. }
  75. }else{//Turn off gzip compression
  76. by bbs.it-home.org
  77. if (file_exists ($filename)) {
  78. $mtime = Filemtime ($filename);
  79. $gmt _mtime = gmdate (' d, D M Y h:i:s ', $mtime). ' GMT ';
  80. if ((Isset ($_server[' http_if_modified_since ')) &&
  81. Array_shift (Explode ('; ', $_server[' http_if_modified_since ')) = = = $gmt _mtime)
  82. ){
  83. Header ("http/1.1 304 not Modified");
  84. Header ("Expires:");
  85. Header ("Cache-control:");
  86. Header ("Pragma:");
  87. Header ($type);
  88. Header ("Tips:cache not Modified");
  89. Header (' content-length:0 ');
  90. }else{
  91. Header ("last-modified:".) $gmt _mtime);
  92. Header ("Expires:");
  93. Header ("Cache-control:");
  94. Header ("Pragma:");
  95. Header ($type);
  96. Header ("Tips:normal Respond");
  97. $content = ReadFile ($filename);
  98. Echo $content;
  99. }
  100. }else{
  101. Header ("http/1.0 404 Not Found");
  102. }
  103. }
  104. ?>
Copy Code

Next, add the following rule in. htaccess (Apache mod_rewrite) or Httpd.ini (IIS isapi_rewrite):

    1. Rewriterule (. *.css$|. *.js$)/gzip.php\?$1 [L]
Copy Code

Finally, the test is performed. Visit each page of the site to see if there is a cache file generation in the Gzip_cache folder. You can also use Baidu Webmaster tools, you can see whether the Css/js page compression.

  • Related Article

    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.