


htaccess PHP+htaccess implements GZIP compression and transmission of static HTML files throughout the site
The power of apache finally exceeded my imagination. I just scratched the surface of php, and this surface 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 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). This thing has been researched by a group of experts, because gzip itself It is well-known and has open source compression principles with high compression rates, so our open source apache uses this open source compression technology.
Well, this .htaccess is also a powerful 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 the URL rewriting module (Module mod_rewrite. c), you can write some URL rewriting code to implement your file rewriting.
Knowledge dissemination completed. . . .
Let’s get to the point.
How to make 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 an 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 )
.htaccess:
Copy code The code is as follows:
# Turn on RewriteEngine mode
RewriteEngine On
RewriteBase /
RewriteRule index2.html l.php?fn=index2.html
1. php
Copy code 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(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 "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},还望高手赐教。
以上就介绍了htaccess PHP+htaccess实现全站静态HTML文件GZIP压缩传输一,包括了htaccess方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
