Home  >  Article  >  Backend Development  >  How to enable gzip compression in php virtual host

How to enable gzip compression in php virtual host

WBOY
WBOYOriginal
2016-07-25 08:58:54810browse
  1. define('ABSPATH', dirname(__FILE__).'/');

  2. $cache = true;//Gzip compression Switch

  3. $cachedir = 'gzip_cache/';//The directory where gz files are stored is 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');< /p>
  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 file extension< ;/p>
  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. // If the file has not changed, return 304
  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)){ //There is no 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 rules in .htaccess (Apache mod_rewrite) or httpd.ini (IIS ISAPI_Rewrite):

  1. RewriteRule (.*.css$|.*.js$) /gzip.php?$1 [L]
Copy the code

Finally, test it. Visit each page of the website to see if there are cache files generated in the gzip_cache folder. You can also use Baidu Webmaster Tools to see whether the css/js page is compressed.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn