HTML文件能不能用GZIP压缩

如何让自己的全站的真实的静态的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.html1.php
<?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 browserif ( $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;
?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值