【发布时间】:2012-09-19 15:12:25
【问题描述】:
假设您的服务器不支持 Apache 上的 deflate 和 gzip 模块。在这种情况下,有几种方法可以压缩您的数据。
我使用 Apache 重写模块和 php gzip 扩展来执行此操作。
我创建了一个名为 gzip.php 的文件来获取 $_SERVER['REQUEST_URI']、获取其内容、设置标题、将内容压缩并刷新为一个文件。
我保留了所有文件的扩展名,因此 apache 保留了文件类型。
我在 .htaccess 中添加了这些行:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^((.*)\.(js|css))$ gzip.php [L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
我已将这些标题添加到我的文件中:
$src = $_SERVER['REQUEST_URI'];
// seconds, minutes, hours, days
$expires = 60*60*24*14;
ob_start();
ob_implicit_flush(0);
header("Pragma: public");
header("Cache-Control: maxage=".$expires);
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');
// Then do everything you want to do on the page
$path = PUBLIC_DIR . $src;
$info = pathinfo($path);
$ext = strtolower($info['extension']);
include_once 'Entezar/File.php';
$mimeType = Entezar_File::getMimeType($path);
header("Content-type: $mimeType");
if (file_exists($path) && in_array($ext, array('js', 'css'))) {
$fs = stat($path);
header("Etag: ".sprintf('"%x-%x-%s"', $fs['ino'], $fs['size'],base_convert(str_pad($fs['mtime'],16,"0"),10,16)));
echo file_get_contents($path);
}
unset($path, $src, $info, $ext);
我的问题是,当我使用 apache 重写模块和 php 来压缩内容时,FireFox 根本不会从缓存中加载我的文件(css 或 js)! 谁能帮帮我?!
【问题讨论】:
-
您是否在浏览器中测试过服务器响应标头?响应标头是否正确?响应正文是否包含您期望的相同内容?
-
感谢Farzad,现在问题已经解决了!我自己回答的!哦,Zend2 你快乐!
标签: php apache optimization mod-rewrite gzip