使用.htaccess和php实现css、js、html的gzip

网站到现在大部分功能都已经完善了,而且订单也越来越多,一直以来访问速度都是个问题,所以在网上找遍了关于加速访问压缩的资料,终于也算是搞定了Speed Page上的提示也OK了

第一种方法:
首先编辑.htaccess,在里面添加两行代码:

  1. rewritecond %{http_host} ^marseo.com [nc]
  2. rewriterule ^(.*)$ http://www.marseo.com/$1 [r=301,nc]

然后在你指定的地址新建一个gzip.php,在里面加上如下代码:

  1. <?php
  2. ob_start("ob_gzhandler");
  3. ob_start("compress");
  4. ?>


第二种方法:
首先,更改.htaccess,添加下面内容

  1. #打开zlib压缩输出,自动压缩html,php文件
  2. php_value zlib.output_compression 2048
  3. #设置css和js文件压缩
  4. <IfModule mod_rewrite.c>
  5. RewriteEngine On
  6. #下面是你要压缩目录的路径,这里为当前
  7. RewriteBase /
  8. RewriteCond %{REQUEST_FILENAME} -f
  9. #下面是gzip.php文件压缩js和css文件,注意gzip.php文件位置
  10. RewriteRule ^(.*)(js|css)$ /gzip.php?file=$1$2&type=$2 [L]
  11. </IfModule>

创建gzip.php文件,位置与你在.htaccess里设置的相同,添加如下内容:

  1. <?php
  2. $file=$_REQUEST['file'];
  3.  
  4. switch ($_REQUEST['type']){
  5. case 'css':
  6. header("Content-type: text/css");
  7. break;
  8. case 'js' :
  9. header("Content-type: text/javascript");
  10. break;
  11. }
  12. include($file);
  13. ?>

Ryan 发表于 星期二, 五月/4/2010 | SEO技术 | 1 条留言
标签:

AddThis Social Bookmark Button

“使用.htaccess和php实现css、js、html的gzip”的1 条留言

  1. 支持喽!博客不错!

Leave a Reply