我已经学过一些关于如何gzip一个css文件的教程,你可以在其中创建一个公共php文件来包含压缩的css文件.问题是我无法让它缓存我的css文件.我正在使用firebug作为参考,我实际上尝试使用相同的代码来压缩一些
javascript并且它可以很好地缓存它.
这是代码:
if(extension_loaded('zlib')){
ob_start('ob_gzhandler');
}
$offset = 60 * 60 * 24 * 31;
header('Content-type: text/css');
header ('Cache-Control: max-age=' . $offset . ', must-revalidate');
header ('Expires: ' . gmdate ("D, d M Y H:i:s", time() + $offset) . ' GMT');
ob_start("compress");
function compress($buffer) {
// Remove Comments, White Space, End ;'s
$buffer = preg_replace('#/\*.*?\*/#s', '', $buffer);
$buffer = preg_replace('/\s*([{}|:;,])\s+/', '$1', $buffer);
$buffer = preg_replace('/\s\s+(.*)/', '$1', $buffer);
$buffer = str_replace(';}', '}', $buffer);
$buffer = str_replace(' {', '{', $buffer);
return $buffer;
}
include('global.css');
if(extension_loaded('zlib')){
ob_end_flush();
}
然后我只是将我的php文件作为css文档引用到其他页面上.
正如你所看到的,我已经尝试将最大年龄添加到混合中,这也证明是不成功的.
以下是响应标头
Date
Tue, 21 Jul 2009 19:59:19 GMT
Server
Apache/1.3.41 (Darwin) PHP/4.4.9
X-Powered-By
PHP/4.4.9
Cache-Control
max-age=2592000, must-revalidate
Expires
Thu, 20 Aug 2009 19:59:19 GMT
Content-Encoding
gzip
Vary
Accept-Encoding
Keep-Alive
timeout=15, max=93
Connection
Keep-Alive
Transfer-Encoding
chunked
Content-Type
text/css
有什么我想念的,或者更好的方法去做这件事吗?
谢谢,
编辑:
检测文件是否已更改的脚本&发送304(如果没有),结合正确的标题已解决了这个问题.
亚瑟