Php+.htaccess implementation of full station static HTML file gzip compression transmission (i) _php skills

Source: Internet
Author: User
Tags pack knowledge base
Apache Strong finally beyond my imagination, just a little bit of a superficial touch of php fur, this fur in my original knowledge base exploded, like the PN knot "avalanche breakdown", let me think of a variety of technologies combined with unlimited application prospects.

As Kyushu's future servers limit traffic, reducing the flow load can reduce the cost of money.
How to reduce traffic, the most convenient way is to use gzip compression, this Apache gzip compression by a called Zlib Class Library and gzip module (MOD_GZIP.C) completed, this thing has a group of cattle research, because Gzip itself is famous, and has a high compression rate of open source compression principle, so our open source Apache will use this open source compression technology.

Well, this one. Htaccess is also an Apache cow, which is too powerful to decide what you can write in this file based on what module your Apache installed, such as the URL rewrite module you installed (modular Mod_ REWRITE.C), you can write some URL rewrite code to implement your file rewrite.

The popularization of knowledge is complete ....

Get to the point.

How do you make your own full station of the real static HTML file that becomes gzip transmitted?
In order to understand the convenience, I wrote a simple PHP program for you.
First we set up a l.php using the gzip compression algorithm, read the xxx.html in the file and display it, then rewrite the xxx.html to 1.php in the. htaccess. It's easy. Because our server thinks that. htaccess has the highest priority, it accesses the xxx.html without accessing the static file, instead accessing the 1.php.

Here's My Code: (read index2.html and rewrite it)
. htaccess:

Copy Code code as follows:

# Open the Rewriteengine mode
Rewriteengine on
Rewritebase/
Rewriterule index2\.html l.php?fn=index2.html



1.php

Copy Code code 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 "\x1f\x8b\x08\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, which is to use this

Rewritecond%{request_filename}!-f
Rewritecond%{request_filename}!-d
Rewriterule. /xxx/xxx.php [L]


But I have not yet studied how to deal with this%{request_filename}, but also hope Master enlighten.

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.