Home Backend Development PHP Tutorial PHP+.htaccess realizes GZIP compression and transmission of static HTML files in the whole site (1)_PHP tutorial

PHP+.htaccess realizes GZIP compression and transmission of static HTML files in the whole site (1)_PHP tutorial

Jul 21, 2016 pm 03:57 PM
apache gzip html one transmission compression accomplish powerful document of static

The power of apache finally exceeded my imagination. I only touched a little bit of PHP's fur like a dragonfly. This fur exploded on the basis of my original knowledge base, like the "avalanche breakdown" of the PN junction, which made me think It combines a variety of technologies with unlimited application prospects.

Since Kyushu’s future servers limit traffic, reducing the traffic load can also reduce money expenditures.
The most convenient way to reduce traffic is to use Gzip compression. The gzip compression of apache is accomplished by a class library called zlib and the gzip module (mod_gzip.c). There are a group of experts who specialize in research on this thing, because gzip itself is famous and has high compression rate open source compression principle, so our open source apache will adopt this open source compression technology.

Well, this .htaccess is also an awesome tool for apache. It is so powerful. It also depends on what modules your apache has installed to determine what you can write in this file. For example, if you have installed URL rewriting Module (Module mod_rewrite.c), you can write some URL rewriting code to implement your file rewriting.

Knowledge dissemination completed. . . .

Get to the point.

How to convert the real static html files of your whole site into gzip transmission?
For the convenience of understanding, I wrote a simple php program for everyone.
First we create a l.php using gzip compression algorithm, read xxx.html in the file and display it, and then rewrite xxx.html to 1.php in .htaccess. Keep it simple. Since our server considers .htaccess to have the highest priority, when accessing xxx.html, this static file is not accessed, but 1.php is accessed.

The following is my code: (read in index2. html, and then rewrite it)
.htaccess:

Copy the code The code is as follows:

# Will RewriteEngine mode on
RewriteEngine On
RewriteBase /
RewriteRule index2.html l.php?fn=index2.html



1.php

Copy code The code is as follows:

<?php 
   $phpver = phpversion(); 

   $useragent = (isset($_SERVER["HTTP_USER_AGENT"]) ) ? $_SERVER["HTTP_USER_AGENT"] : $HTTP_USER_AGENT; 

   if ( $phpver >= '4.0.4pl1' && ( strstr($useragent,'compatible') || strstr($useragent,'Gecko') ) ) 
   { 
       if ( extension_loaded('zlib') ) 
       { 
           ob_start('ob_gzhandler'); 
       } 
   } 
   else if ( $phpver > '4.0' ) 
   { 
       if ( strstr($HTTP_SERVER_VARS['HTTP_ACCEPT_ENCODING'], 'gzip') ) 
       { 
           if ( extension_loaded('zlib') ) 
           { 
               $do_gzip_compress = TRUE; 
               ob_start(); 
               ob_implicit_flush(0); 

               header('Content-Encoding: gzip'); 
           } 
       } 
   } 
?> 
<?php 
$rfile = addslashes(dirname(dirname(__FILE__))).'/'.'./httpdocs/'.$_REQUEST['fn']; 
echo READ_FILE_CONTENTS($rfile); 
function READ_FILE_CONTENTS($file) 

   if(!function_exists("file_get_contents"))return file_get_contents($file); 
   $ifile = fopen($file,"r"); 
   $contents = false; 
   if($ifile) while (!feof($ifile)) $contents .= fgets($ifile); 
   fclose($ifile); 
   return $contents; 

?> 
<?php 
// Compress buffered output if required and send to browser 
if ( $do_gzip_compress ) 

   // 
   // Borrowed from php.net! 
   // 
   $gzip_contents = ob_get_contents(); 
   ob_end_clean(); 

   $gzip_size = strlen($gzip_contents); 
   $gzip_crc = crc32($gzip_contents); 

   $gzip_contents = gzcompress($gzip_contents, 9); 
   $gzip_contents = substr($gzip_contents, 0, strlen($gzip_contents) - 4); 

   echo "x1fx8bx08x00x00x00x00x00"; 
   echo $gzip_contents; 
   echo pack('V', $gzip_crc); 
   echo pack('V', $gzip_size); 


exit; 
?> 



实际上这个东西能用更好的方法解决,就是用这个

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /xxx/xxx.php [L]


但是我还没研究出来怎么处理这个%{REQUEST_FILENAME},还望高手赐教。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/317907.htmlTechArticleapache的强大终于超出了我的想象,仅仅蜻蜓点水般触及了一点php皮毛,这点皮毛就在我原有的知识库基础上爆炸开来,好像PN结的“雪崩击穿...
Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

Nested Table in HTML

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Table Border in HTML

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

HTML margin-left

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

HTML Table Layout

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Moving Text in HTML

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

HTML Ordered List

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

How do you parse and process HTML/XML in PHP?

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

HTML onclick Button

See all articles