PHP +. htaccess implements full-site static HTML file GZIP compression and transmission (I) apache's strength is beyond my imagination, and it just touched a bit of php, this explosion in my original knowledge base is like the avalanche breakdown of the pnknot, which reminds me of the unlimited application prospect of combining multiple technologies.
Because Kyushu's servers will limit traffic in the future, reducing traffic load will reduce the cost of money.
The most convenient way to reduce traffic is to use Gzip compression. The gzip compression of apache relies on a zlib class library and a gzip module (mod_gzip.c, this stuff is specially researched by a group of cool people. because gzip itself is well-known and has a high compression ratio and open source compression principle, our open source apache will adopt this open source compression technology.
Well, this. htaccess is also a powerful tool for apache. it also determines what you can write in this file based on the modules installed in apache, for example, if you have installed the URL rewriting Module (Module mod_rewrite.c), you can write some URL rewriting code to rewrite your file.
Knowledge popularization is complete ....
Go to the topic.
How can we convert the real static html files on the entire site into gzip files?
For ease of understanding, I wrote a simple php program.
Rewrite to 1. php. Simple. Because our server recognizes .htaccessas the highest priority, the static file is not accessed when "xxx.html" is used, but 1. php.
The following is my code: (read index2.html and then rewrite it)
. Htaccess:
The code is as follows:
# Enable RewriteEngine mode
RewriteEngine On
RewriteBase/
RewriteRule index2 \. html l. php? Fn1_index2.html
1. php
The code is as follows:
$ 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 ');
}
}
}
?>
$ Rfile = addslashes (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;
}
?>
// 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 "\ x1f \ x8b \ x08 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 ";
Echo $ gzip_contents;
Echo pack ('v', $ gzip_crc );
Echo pack ('v', $ gzip_size );
}
Exit;
?>
In fact, this thing can be solved in a better way.
RewriteCond % {REQUEST_FILENAME }! -F
RewriteCond % {REQUEST_FILENAME }! -D
RewriteRule./xxx. php [L]
But I haven't figured out how to deal with this % {REQUEST_FILENAME} yet.