search
HomeBackend DevelopmentPHP TutorialPHP+.htaccess实现全站静态HTML文件GZIP压缩传输(一)_php技巧

apache的强大终于超出了我的想象,仅仅蜻蜓点水般触及了一点php皮毛,这点皮毛就在我原有的知识库基础上爆炸开来,好像PN结的“雪崩击穿”一样,让我想到了多种技术结合无限的应用前景。

由于九州未来的服务器限制流量,那么减少流量负载也就能减少金钱支出。
如何减少流量,最方便的办法就是用Gzip压缩,这个apache的gzip压缩是靠一个叫做zlib的类库和gzip的模块(mod_gzip.c)完成的,这玩意专门有一帮牛人研究,因为gzip本身就大名鼎鼎的,并且具有高压缩率开源的压缩原理,所以我们的开源apache才会采用这种开源的压缩技术。

恩,这个.htaccess也是apache的一个牛比东西,太强大了,也是根据你的apache安装了什么模块而决定你这个文件里面可以写什么东西,比如你安装了URL重写模块(Module mod_rewrite.c)的话你就可以写一些URL重写代码来实现你的文件重写。

知识普及完毕。。。。

进入正题。

如何让自己的全站的真实的静态的html文件,变成gzip传输的呢?
为了理解方便,我给大家写了一个简单的php程序。
首先我们建立一个采用gzip压缩算法的l.php,在该文件中读入xxx.html并显示出来,然后再在.htaccess里面重写xxx.html到1.php就可以了。简单吧。由于我们的服务器认为.htaccess的优先级最高,所以访问xxx.html的时候没有访问到这个静态文件,反而访问到了1.php.

下面是我的代码:(读入index2.html,然后重写之)
.htaccess:

复制代码 代码如下:

# 将 RewriteEngine 模式打开 
RewriteEngine On 
RewriteBase / 
RewriteRule index2\.html l.php?fn=index2.html 



1.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'); 
           } 
       } 
   } 
?> 
$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; 
?> 



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

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


但是我还没研究出来怎么处理这个%{REQUEST_FILENAME},还望高手赐教。
Statement
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
Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Build a React App With a Laravel Back End: Part 2, ReactBuild a React App With a Laravel Back End: Part 2, ReactMar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Notifications in LaravelNotifications in LaravelMar 04, 2025 am 09:22 AM

In this article, we're going to explore the notification system in the Laravel web framework. The notification system in Laravel allows you to send notifications to users over different channels. Today, we'll discuss how you can send notifications ov

Explain the concept of late static binding in PHP.Explain the concept of late static binding in PHP.Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

PHP Logging: Best Practices for PHP Log AnalysisPHP Logging: Best Practices for PHP Log AnalysisMar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor