想象一下,您的服务器不支持Apache上的deflate和gzip模块 . 在这种情况下,有几种方法可以压缩数据 .
我使用Apache重写模块和php gzip扩展来执行此操作 .
我创建了一个名为gzip.php的文件来获取$ _SERVER ['REQUEST_URI'],获取其内容,设置 Headers ,压缩并将内容刷新为文件 .
我保留了所有文件的扩展名,因此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]
我已将这些 Headers 添加到我的文件中:
$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);
我的问题是,当我在php中使用apache重写模块来压缩内容时,FireFox根本不会从缓存中加载我的文件(css或js)!有谁能够帮我?!
1 回答
挖掘机的发现者!在做任何工作(压缩gzip.php文件中的一些文件)之前,你应该检查$ _SERVER变量中的这两个键(当然你应该在apache .htaccess文件或其他地方设置expiration和缓存头文件......):
在apache .htaccess中添加以下行: