Htaccess Php+htaccess implements the full station static HTML file gzip compressed transmission one

Source: Internet
Author: User
Tags knowledge base
Apache's power finally beyond my imagination, just Dragonfly water touch a little php fur, this fur on the basis of my original knowledge base exploded, as if 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 traffic load can also reduce the cost of money.
How to reduce traffic, the most convenient way is to use gzip compression, this Apache gzip compression is a class library called Zlib and gzip module (MOD_GZIP.C) done, this thing specifically a bunch of cattle people study, because Gzip itself is famous, And with a high compression rate of open source compression principle, so our open-source Apache will adopt this open-source compression technology.
Well, this. Htaccess is also an Apache bull than something, too powerful, but also according to your Apache installed what module to decide what you can write in this file, for example, you installed a URL rewrite module mod_ REWRITE.C) Then you can write some URL rewrite code to implement your file rewrite.
Knowledge popularization is complete ....
Go to the chase.
How to make your own full station of the real static HTML file, into the gzip transfer it?
For ease of understanding, I wrote a simple PHP program for everyone.
First we create a l.php using the gzip compression algorithm, read the xxx.html in the file and display it, and then rewrite xxx.html to 1.php in the. htaccess. It's simple. Since our server believes that the. 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 in index2.html and rewrite it)
. htaccess:

Copy the Code code as follows:


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


1.php

Copy the Code code 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 (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";
echo $gzip _contents;
Echo Pack (' V ', $gzip _CRC);
Echo Pack (' V ', $gzip _size);
}
Exit
?>


Actually, this thing can be solved in a better way.
Rewritecond%{request_filename}!-f
Rewritecond%{request_filename}!-d
Rewriterule. /xxx/xxx.php [L]
But I have not studied how to deal with this%{request_filename}, but also look at the master enlighten.

The above describes the htaccess php+htaccess implementation of the full station static HTML file gzip compression transmission, including the htaccess aspects of the content, I hope that the PHP tutorial interested friends have helped.

  • 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.