gzip.php无缓存版

有缓存那个gzip始终有问题,干脆换成这个无缓存版本。

本gzip.php默认压缩输出css、js、html、xml等内容。

Rewrite规则:

#gzip for non-PHP
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} ^.*\.(css|js|html|htm|xml|/)$
RewriteRule ^(.*)$ gzip.php?url=$1 [QSA,L]
</IfModule>
#END gzip for non-PHP

gzip.php文件代码内容:

<?php
$allowed = array(
'css' => 'text/css',
'js' => 'application/x-javascript',
'html' => 'text/html',
'htm' => 'text/html',
'xml' => 'text/xml',
);
$file = isset($_GET['url']) ? $_GET['url'] : null;
$extension = explode('.', $file);
$extension = array_pop($extension);
if(isset($allowed[$extension]))
{
$pos = strpos($file, '..');
if ($pos === false && is_file($file))
{
@ob_start ('ob_gzhandler');
header("Content-type: {$allowed[$extension]}; charset: UTF-8");
readfile($file);
} else {
header('HTTP/1.1 404 Not Found');
}
}
?>

4 comments

  1. mickey says:

    ~~~~~~~~~~~~完全看不懂啥东东~~~~~

    1. creke says:

      你看得懂那还了得……

  2. mickey says:

    是是是,你就拽吧~~~~~

  3. Yang says:

    带缓存的出问题应该是因为你将HTML静态页面都压缩的问题吧...
    CSS和JS压缩就好了如果html也要压缩那许多动态交互页面就会出问题了..

Leave a Reply to mickey Cancel reply