问题描述
|
我的托管公司不支持mod_gzip或mod_deflate。
我必须gzip .js和CSS文件
我已经预压缩了文件。
我使用Joomla,并将脚本添加到文档中,只要用户接受了编码,就必须使用提供.js.gz文件的PHP文件(PHP joomla代码示例):
$document = &JFactory::getDocument();
$document->addScript($jQueryPath);
我在.htaccess中添加了以下指令;但是,浏览器未解压缩.js.gz文件
# Add some gzip to the bunch
AddEncoding x-gzip .gz
<FilesMatch \\.js.gz$>
ForceType text/javascript
Header set content-encoding \"gzip\"
</FilesMatch>
<FilesMatch \\.css.gz$>
ForceType text/css
Header set content-encoding \"gzip\"
</FilesMatch>
RewriteCond %{REQUEST_FILENAME} ^.+\\.(js|css)$ [NC]
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule ^(.+)$ $1.gz [L,QSA,NC,NS]
在上述条件下,有人可以提供有关如何gzip的帮助吗?
谢谢,
解决方法
大多数php安装都带有Zlib扩展,
使您可以操纵gzip文件。
看看这些php手册页,以完成您所要求的:
http://www.php.net/manual/zh/function.gzcompress.php
http://www.php.net/manual/zh/function.gzdeflate.php
http://www.php.net/manual/zh/function.gzencode.php
您可以使用$ _SERVER超全局变量来检查Accept Encoding标头:
if (substr_count($_SERVER[\'HTTP_ACCEPT_ENCODING\'],\'deflate\'))
{
// DO STUFF HERE
}
elseif (substr_count($_SERVER[\'HTTP_ACCEPT_ENCODING\'],\'gzip\'))
{
// DO STUFF HERE
}