<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>阅微堂 &#187; GZIP</title>
	<atom:link href="http://zhiqiang.org/blog/tag/gzip/feed" rel="self" type="application/rss+xml" />
	<link>http://zhiqiang.org/blog</link>
	<description>理工科背景的证券从业人员</description>
	<lastBuildDate>Sun, 05 Feb 2012 03:59:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>设置blog的缓存和压缩</title>
		<link>http://zhiqiang.org/blog/it/using-htaccess-to-speed-blog.html</link>
		<comments>http://zhiqiang.org/blog/it/using-htaccess-to-speed-blog.html#comments</comments>
		<pubDate>Tue, 08 Dec 2009 12:59:36 +0000</pubDate>
		<dc:creator>zhiqiang</dc:creator>
				<category><![CDATA[IT技术]]></category>
		<category><![CDATA[GZIP]]></category>
		<category><![CDATA[缓存]]></category>
		<category><![CDATA[速度优化]]></category>

		<guid isPermaLink="false">http://zhiqiang.org/blog/posts/using-htaccess-to-speed-blog.html</guid>
		<description><![CDATA[博客 » IT技术 » 缓存 » 在网站根目录下的.htaccess文件中添加以下代码 &#60;IfModule mod_expires.c&#62; ExpiresActive On ExpiresByType image/gif A2592000 ExpiresByType image/jpeg A2592000 ExpiresByType image/png A2592000 ExpiresByType application/x-shockwave-flash A2592000 ExpiresByType text/css A2592000 ExpiresByType application/x-javascript A2592000 ExpiresByType application/javascript A2592000 ExpiresByType text/javascript A2592000 ExpiresByType text/html A600 &#60;/IfM...]]></description>
			<content:encoded><![CDATA[<p id="breadcrumb" class="breadcrumb"><a href="http://zhiqiang.org/blog/">博客</a> » <a href="http://zhiqiang.org/blog/category/it">IT技术</a> » <a href='http://zhiqiang.org/blog/tag/%e7%bc%93%e5%ad%98'>缓存</a>  » </p><p>在网站根目录下的.htaccess文件中添加以下代码</p>
<pre><span style="color: #0000ff">&lt;</span><span style="color: #800000">IfModule</span> <span style="color: #ff0000">mod_expires</span>.<span style="color: #ff0000">c</span><span style="color: #0000ff">&gt;</span>
ExpiresActive On
ExpiresByType image/gif A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType image/png A2592000
ExpiresByType application/x-shockwave-flash A2592000
ExpiresByType text/css A2592000
ExpiresByType application/x-javascript A2592000
ExpiresByType application/javascript A2592000
ExpiresByType text/javascript A2592000
ExpiresByType text/html A600
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">IfModule</span><span style="color: #0000ff">&gt;</span>

<span style="color: #0000ff">&lt;</span><span style="color: #800000">IfModule</span> <span style="color: #ff0000">mod_deflate</span>.<span style="color: #ff0000">c</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">FilesMatch</span> &quot;\.(<span style="color: #ff0000">js</span>|<span style="color: #ff0000">css</span>|<span style="color: #ff0000">html</span>|<span style="color: #ff0000">htm</span>)$&quot;<span style="color: #0000ff">&gt;</span>
SetOutputFilter DEFLATE
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">FilesMatch</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">IfModule</span><span style="color: #0000ff">&gt;</span>

<span style="color: #0000ff">&lt;</span><span style="color: #800000">ifModule</span> <span style="color: #ff0000">mod_gzip</span>.<span style="color: #ff0000">c</span><span style="color: #0000ff">&gt;</span>
  mod_gzip_on Yes
  mod_gzip_dechunk Yes
  mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
  mod_gzip_item_include handler ^cgi-script$
  mod_gzip_item_include mime ^text/.*
  mod_gzip_item_include mime ^application/x-javascript.*
  mod_gzip_item_exclude mime ^image/.*
  mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">ifModule</span><span style="color: #0000ff">&gt;</span>

<span style="color: #0000ff">&lt;</span><span style="color: #800000">FilesMatch</span> &quot;\.(<span style="color: #ff0000">ico</span>|<span style="color: #ff0000">pdf</span>|<span style="color: #ff0000">flv</span>|<span style="color: #ff0000">jpg</span>|<span style="color: #ff0000">jpeg</span>|<span style="color: #ff0000">png</span>|<span style="color: #ff0000">gif</span>|<span style="color: #ff0000">js</span>|<span style="color: #ff0000">css</span>)$&quot;<span style="color: #0000ff">&gt;</span>
Header unset Last-Modified
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">FilesMatch</span><span style="color: #0000ff">&gt;</span>

FileETag none</pre>
<p>现在使用Firefox的YSlow插件，以Small Site or Blog标准测试的分数为99分，以YSlow2(V2)标准测试的分数为94分，被扣掉的几分主要是因为没有使用CDN。所以现在<a href="http://zhiqiang.org/blog/it/speedup-your-blog.html">速度已经被优化到了极致</a>。</p>
<p>赶快把它放到你的网站或blog根目录下的.htaccess文件里吧。</p>
<div><h4>相关文章</h4><ul><li><a href="http://zhiqiang.org/blog/it/speedup-blog-gzip-javascript-css.html">加速blog：GZIP压缩传输你的文件</a></li><li><a href="http://zhiqiang.org/blog/it/dreamhost-on-gzip-cache.html">Dreamhost上的GZIP &#038; Cache</a></li><li><a href="http://zhiqiang.org/blog/it/speedup-website-with-css-sprites.html">用CSS Sprites让网站再加速</a></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-set-browser-cache.html">加速blog：设置浏览器缓存</a></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-cache-on-server.html">加速blog：服务器端的中转和缓存</a></li></ul></div>    <p></p>
    <hr noshade style="margin:0;height:1px" />
    <p>&copy; zhiqiang for <a href="http://zhiqiang.org/blog">阅微堂</a>, 2009. | <a href="http://zhiqiang.org/blog/it/using-htaccess-to-speed-blog.html">&#38142;&#25509;</a> | <a href="http://zhiqiang.org/blog/it/using-htaccess-to-speed-blog.html#comments">17 &#26465;&#35780;&#35770;</a></p>]]></content:encoded>
			<wfw:commentRss>http://zhiqiang.org/blog/it/using-htaccess-to-speed-blog.html/feed</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>加速blog：GZIP压缩传输你的文件</title>
		<link>http://zhiqiang.org/blog/it/speedup-blog-gzip-javascript-css.html</link>
		<comments>http://zhiqiang.org/blog/it/speedup-blog-gzip-javascript-css.html#comments</comments>
		<pubDate>Thu, 29 Nov 2007 10:43:50 +0000</pubDate>
		<dc:creator>zhiqiang</dc:creator>
				<category><![CDATA[IT技术]]></category>
		<category><![CDATA[GZIP]]></category>
		<category><![CDATA[压缩]]></category>
		<category><![CDATA[速度优化]]></category>

		<guid isPermaLink="false">http://zhiqiang.org/blog/?p=706</guid>
		<description><![CDATA[博客 » IT技术 » GZIP » 在浏览网页的时候，浏览器需要把网页上的内容，包括网页HTML，CSS，JS，FLASH和图片等下载到本地，然后把它们显示出来。如果网页上的内容是固定的，下载速度也不是你我能控制的，但下载的大小则是可以控制的，只需要你再传送的过程中先把内容压缩一下即可。 对于一般的HTML，CSS和JS文件，GZIP的压缩比率达50%到80%，节约的流量是非常惊人的。下图是...]]></description>
			<content:encoded><![CDATA[<p id="breadcrumb" class="breadcrumb"><a href="http://zhiqiang.org/blog/">博客</a> » <a href="http://zhiqiang.org/blog/category/it">IT技术</a> » <a href='http://zhiqiang.org/blog/tag/gzip'>GZIP</a>  » </p><p>在浏览网页的时候，浏览器需要把网页上的内容，包括网页HTML，CSS，JS，FLASH和图片等下载到本地，然后把它们显示出来。如果网页上的内容是固定的，下载速度也不是你我能控制的，但下载的大小则是可以控制的，只需要你再传送的过程中先把内容压缩一下即可。</p>
<p>对于一般的HTML，CSS和JS文件，GZIP的压缩比率达50%到80%，节约的流量是非常惊人的。下图是阅微堂上的GZIP结果：</p>
<p style="text-align: center"><img src="http://lh3.ggpht.com/mathzqy/RzlfaqQHJ-I/AAAAAAAABqA/EAWWcc09txY/s800/20071113162512812.jpg"/></p>
<h3>动态内容压缩</h3>
<p>动态内容指动态脚本比如PHP生成的内容，比如WordPress产生的内容均为动态脚本内容。对于动态脚本的压缩方式有两种，一种是ob_gzhandler，一种是zlib.output_compression。前者几乎所有服务器都支持，只需要在WordPress后台options-&gt;Reading-&gt;WordPress should compress articles (gzip) if browsers ask for them打开GZIP传输即可。而后者是一个PHP模块，效果更佳，因为它是边生成，边压缩，边传输的，效率和速度都更好。但这个是PHP的一个模块，不是所有服务器上的有这个模块。</p>
<h3>静态文本压缩</h3>
<p>但对于另外一些需要压缩的静态文本，比如html, css, javascript文件等。相应的静态文本压缩的Apache模块有两种，GZIP和DEFLATE，后者更好，不过这都没啥选择，完全看服务器是否提供，个人无法配置，比如使用最多的Bluehost和Dreamhost都不支持。</p>
<h3>静态文本调用PHP压缩模块实现GZIP压缩效果</h3>
<p>如果服务器不提供对于静态文本的GZIP压缩模块，唯一的出路是通过调用PHP。有好几种方法调用PHP达到这个效果，下面介绍一种方法，好处是除了在目录下添加一个文件和修改.htaccess之外，不需要对别的代码作任何修改，所有的js，javascript的压缩都自动生效。（不过，如果你看不懂下面这些代码为什么能起作用，就不要使用这个方法）</p>
<p>首先，修改你的blog目录下的.htaccess文件，加入下面几行代码(注意修改你的blog的目录，这里默认是/blog/)</p>
<blockquote><pre># to gzip your css, javascript, html file automatically
<span style="color: #0000ff">&lt;</span><span style="color: #800000">IfModule</span> <span style="color: #ff0000">mod_rewrite</span>.<span style="color: #ff0000">c</span><span style="color: #0000ff">&gt;</span>
RewriteEngine On
RewriteBase /blog/ #directory of your blog, change it to yours
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)(js|css|html|htm)$ /blog/redir.php?file=$1$2&amp;type=$2 [L]
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">IfModule</span><span style="color: #0000ff">&gt;</span></pre>
</blockquote>
<p>其次，将下面代码保存在文件redir.php里，放到blog的根目录</p>
<pre><span style="color: #0000ff">&lt;?</span>php <span style="color: #008000"># this is the file redir.php, to gzip javascript and css</span>
<span style="color: #008000"># set the request file name</span>
$<a style="color: #ffa500" href="http://cn.php.net/manual/en/function.file.php">file</a>=$_REQUEST['<span style="color: #8b0000">file</span>'];

<span style="color: #008000"># Set Expires, cache the file on the browse</span>
<span style="color: #008000"># Delete it if you don't want it</span>
<a style="color: #ffa500" href="http://cn.php.net/manual/en/function.header.php">header</a>("<span style="color: #8b0000">Expires:</span>".<a style="color: #ffa500" href="http://cn.php.net/gmdate">gmdate</a>("<span style="color: #8b0000">D, d M Y H:i:s</span>", <a style="color: #ffa500" href="http://cn.php.net/time">time</a>()+15360000)."<span style="color: #8b0000">GMT</span>");
<a style="color: #ffa500" href="http://cn.php.net/manual/en/function.header.php">header</a>("<span style="color: #8b0000">Cache-Control: max-age=315360000</span>");

<span style="color: #008000"># set the last modified time</span>
$mtime = <a style="color: #ffa500" href="http://cn.php.net/filemtime">filemtime</a>($<a style="color: #ffa500" href="http://cn.php.net/manual/en/function.file.php">file</a>);
$gmt_mtime = <a style="color: #ffa500" href="http://cn.php.net/gmdate">gmdate</a>('<span style="color: #8b0000">D, d M Y H:i:s</span>', $mtime) . '<span style="color: #8b0000"> GMT</span>';
<a style="color: #ffa500" href="http://cn.php.net/manual/en/function.header.php">header</a>("<span style="color: #8b0000">Last-Modified:</span>" . $gmt_mtime);

<span style="color: #008000"># output a mediatype header</span>
<a style="color: #0000ff" href="http://cn.php.net/manual/en/control-structures.switch.php">switch</a> ($_REQUEST['<span style="color: #8b0000">type</span>']){
  <a style="color: #0000ff" href="http://cn.php.net/manual/en/control-structures.switch.php">case</a> '<span style="color: #8b0000">css</span>':
    <a style="color: #ffa500" href="http://cn.php.net/manual/en/function.header.php">header</a>("<span style="color: #8b0000">Content-type: text/css</span>");
    <a style="color: #0000ff" href="http://cn.php.net/break">break</a>;
  <a style="color: #0000ff" href="http://cn.php.net/manual/en/control-structures.switch.php">case</a> '<span style="color: #8b0000">js</span>' :
    <a style="color: #ffa500" href="http://cn.php.net/manual/en/function.header.php">header</a>("<span style="color: #8b0000">Content-type: text/javascript</span>");
    <a style="color: #0000ff" href="http://cn.php.net/break">break</a>;
  <a style="color: #0000ff" href="http://cn.php.net/manual/en/control-structures.switch.php">case</a> '<span style="color: #8b0000">html</span>':
    <a style="color: #ffa500" href="http://cn.php.net/manual/en/function.header.php">header</a>("<span style="color: #8b0000">Content-type: text/html</span>");
    <a style="color: #0000ff" href="http://cn.php.net/break">break</a>;
  <a style="color: #0000ff" href="http://cn.php.net/manual-lookup.php?pattern=default&amp;lang=en&amp;scope=404quickref">default</a>:
    <a style="color: #ffa500" href="http://cn.php.net/manual/en/function.header.php">header</a>("<span style="color: #8b0000">Content-type: text/plain</span>");
}

<span style="color: #008000"># GZIP the content</span>
<a style="color: #0000ff" href="http://cn.php.net/manual/en/control-structures.if.php">if</a>(<a style="color: #ffa500" href="http://cn.php.net/extension_loaded">extension_loaded</a>('<span style="color: #8b0000">zlib</span>')){<a style="color: #ffa500" href="http://cn.php.net/ob_start">ob_start</a>();<a style="color: #ffa500" href="http://cn.php.net/ob_start">ob_start</a>('<span style="color: #8b0000">ob_gzhandler</span>');}

<span style="color: #008000"># echo the file's contents</span>
<a style="color: #0000ff" href="http://cn.php.net/echo">echo</a> <a style="color: #ffa500" href="http://cn.php.net/implode">implode</a>('<span style="color: #8b0000"></span>', <a style="color: #ffa500" href="http://cn.php.net/manual/en/function.file.php">file</a>($<a style="color: #ffa500" href="http://cn.php.net/manual/en/function.file.php">file</a>));

<a style="color: #0000ff" href="http://cn.php.net/manual/en/control-structures.if.php">if</a>(<a style="color: #ffa500" href="http://cn.php.net/extension_loaded">extension_loaded</a>('<span style="color: #8b0000">zlib</span>')){
  <a style="color: #ffa500" href="http://cn.php.net/ob_end_flush">ob_end_flush</a>();
  <span style="color: #008000"># set header the content's length;</span>
  <span style="color: #008000"># header("Content-Length: ".ob_get_length()); # (It doesn't work? )</span>
  <a style="color: #ffa500" href="http://cn.php.net/ob_end_flush">ob_end_flush</a>();
}
<span style="color: #0000ff">?&gt;</span></pre>
<p>大致解释一下原理，那段.htaccess代码把所有的css,js,html,htm文档自动转向到redir.php，redir.php则读取真实的文件内容，调用PHP压缩模块ob_gzhandler压缩后输出。</p>
<div><h4>相关文章</h4><ul><li><a href="http://zhiqiang.org/blog/it/using-htaccess-to-speed-blog.html">设置blog的缓存和压缩</a></li><li><a href="http://zhiqiang.org/blog/it/dreamhost-on-gzip-cache.html">Dreamhost上的GZIP &#038; Cache</a></li></ul></div>    <p></p>
    <hr noshade style="margin:0;height:1px" />
    <p>&copy; zhiqiang for <a href="http://zhiqiang.org/blog">阅微堂</a>, 2007. | <a href="http://zhiqiang.org/blog/it/speedup-blog-gzip-javascript-css.html">&#38142;&#25509;</a> | <a href="http://zhiqiang.org/blog/it/speedup-blog-gzip-javascript-css.html#comments">25 &#26465;&#35780;&#35770;</a></p>]]></content:encoded>
			<wfw:commentRss>http://zhiqiang.org/blog/it/speedup-blog-gzip-javascript-css.html/feed</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>Dreamhost上的GZIP &amp; Cache</title>
		<link>http://zhiqiang.org/blog/it/dreamhost-on-gzip-cache.html</link>
		<comments>http://zhiqiang.org/blog/it/dreamhost-on-gzip-cache.html#comments</comments>
		<pubDate>Sun, 01 Apr 2007 11:09:54 +0000</pubDate>
		<dc:creator>zhiqiang</dc:creator>
				<category><![CDATA[IT技术]]></category>
		<category><![CDATA[Cache]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[Dreamhost]]></category>
		<category><![CDATA[GZIP]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://zhiqiang.org/blog/553.html</guid>
		<description><![CDATA[博客 » IT技术 » Dreamhost » 用WordPress的都知道，WordPress可以打开GZIP传输，压缩比率能达到60%到80%。下表是本blog首页在Web Page Analyzer上的测试报告，此页面上包括css, js所有东西加起来才14K。 QTY SIZE# TYPE URL 1 6999 HTML http://zhiqiang.org/blog/ 1 2593 SCRIPT http://zhiqiang.org/blog/.../comment.js.php 1 2302 SCRIPT http://zhiqiang.org/.../all.js.php 1 2048 CSS http://zhiqiang.org/.../style.css.php 1 431 IMG http://feeds.feedburner.com/...]]></description>
			<content:encoded><![CDATA[<p id="breadcrumb" class="breadcrumb"><a href="http://zhiqiang.org/blog/">博客</a> » <a href="http://zhiqiang.org/blog/category/it">IT技术</a> » <a href='http://zhiqiang.org/blog/tag/dreamhost'>Dreamhost</a>  » </p><p>用WordPress的都知道，WordPress可以打开GZIP传输，压缩比率能达到60%到80%。下表是本<a href="http://zhiqiang.org/blog/">blog首页</a>在<a href="http://www.websiteoptimization.com/services/analyze/index.html" target="_blank">Web Page Analyzer</a>上的测试报告，此页面上包括css, js<strong>所有东西</strong>加起来才<strong>14K。</strong></p>
<p align="center">
<table class="t" border="1">
<tbody>
<tr>
<th>QTY</th>
<th>SIZE#</th>
<th>TYPE</th>
<th>URL</th>
</tr>
<tr>
<td nowrap>1 </td>
<td nowrap>6999 </td>
<td nowrap>HTML </td>
<td nowrap><a href="http://zhiqiang.org/blog/">http://zhiqiang.org/blog/</a> </td>
</tr>
<tr>
<td nowrap>1 </td>
<td nowrap>2593 </td>
<td nowrap>SCRIPT </td>
<td nowrap><a href="http://zhiqiang.org/blog/wp-content/plugins/ajaxcomment/comment.js.php">http://zhiqiang.org/blog/.../comment.js.php</a> </td>
</tr>
<tr>
<td nowrap>1 </td>
<td nowrap>2302 </td>
<td nowrap>SCRIPT </td>
<td nowrap><a href="http://zhiqiang.org/blog/jscript/all.js.php">http://zhiqiang.org/.../all.js.php</a> </td>
</tr>
<tr>
<td nowrap>1 </td>
<td nowrap>2048 </td>
<td nowrap>CSS </td>
<td nowrap><a href="http://zhiqiang.org/blog/wp-content/themes/phoenixblue2/style.css.php">http://zhiqiang.org/.../style.css.php</a> </td>
</tr>
<tr>
<td nowrap>1 </td>
<td nowrap>431 </td>
<td nowrap>IMG </td>
<td nowrap><a href="http://feeds.feedburner.com/%7Efc/zhiqiang?bg=99CCFF&#038;ampfg=444444&#038;ampanim=0">http://feeds.feedburner.com/~fc/zhiqiang</a> </td>
</tr>
<tr>
<td nowrap>5 ^ </td>
<td nowrap>14373* </td>
<td> </td>
<td>Total (^unique objects)</td>
</tr>
</tbody>
</table>
<p>Dreamhost还用的是Apache 1.3(Am I wrong？)，所以没有mod_deflate模块，而用的是mod_gzip模块，这个模块直接在磁盘上读写，效率比前者要低。不过有总比没有好，毕竟压缩比率很高。</p>
<p>但诡异的是，服务器默认对CSS和JS不作压缩。写信去问了dreamhost客服，也没有什么好方法。最后我采用的方法是用php给包一层：用style.css.php来代替style.css：</p>
<blockquote><pre><span style="color: #0000ff">&lt;?</span>php <span style="color: #008000"># this is the file style.css.php, who contains style.css</span>
<span style="color: #008000"># set the request file name</span>
$<a style="color: #ffa500" href="http://cn.php.net/manual/en/function.file.php">file</a>="<span style="color: #8b0000">style.css</span>";

<span style="color: #008000"># Set Expires, cache the file on the browse</span>
<span style="color: #008000"># Delete it if you don't want it</span>
<a style="color: #ffa500" href="http://cn.php.net/manual/en/function.header.php">header</a>("<span style="color: #8b0000">Expires:</span>".<a style="color: #ffa500" href="http://cn.php.net/gmdate">gmdate</a>("<span style="color: #8b0000">D, d M Y H:i:s</span>", <a style="color: #ffa500" href="http://cn.php.net/time">time</a>()+15360000)."<span style="color: #8b0000">GMT</span>");
<a style="color: #ffa500" href="http://cn.php.net/manual/en/function.header.php">header</a>("<span style="color: #8b0000">Cache-Control: max-age=315360000</span>");

<span style="color: #008000"># set the last modified time</span>
$mtime = <a style="color: #ffa500" href="http://cn.php.net/filemtime">filemtime</a>($<a style="color: #ffa500" href="http://cn.php.net/manual/en/function.file.php">file</a>);
$gmt_mtime = <a style="color: #ffa500" href="http://cn.php.net/gmdate">gmdate</a>('<span style="color: #8b0000">D, d M Y H:i:s</span>', $mtime) . '<span style="color: #8b0000"> GMT</span>';
<a style="color: #ffa500" href="http://cn.php.net/manual/en/function.header.php">header</a>("<span style="color: #8b0000">Last-Modified:</span>" . $gmt_mtime);

<span style="color: #008000"># output a mediatype header</span>
$ext = <a style="color: #ffa500" href="http://cn.php.net/array_pop">array_pop</a>(<a style="color: #ffa500" href="http://cn.php.net/explode">explode</a>('<span style="color: #8b0000">.</span>', $<a style="color: #ffa500" href="http://cn.php.net/manual/en/function.file.php">file</a>));
<a style="color: #0000ff" href="http://cn.php.net/manual/en/control-structures.switch.php">switch</a> ($ext){
<a style="color: #0000ff" href="http://cn.php.net/manual/en/control-structures.switch.php">case</a> '<span style="color: #8b0000">css</span>':
 <a style="color: #ffa500" href="http://cn.php.net/manual/en/function.header.php">header</a>("<span style="color: #8b0000">Content-type: text/css</span>");
 <a style="color: #0000ff" href="http://cn.php.net/break">break</a>;
<a style="color: #0000ff" href="http://cn.php.net/manual/en/control-structures.switch.php">case</a> '<span style="color: #8b0000">js</span>' :
 <a style="color: #ffa500" href="http://cn.php.net/manual/en/function.header.php">header</a>("<span style="color: #8b0000">Content-type: text/javascript</span>");
 <a style="color: #0000ff" href="http://cn.php.net/break">break</a>;
<a style="color: #0000ff" href="http://cn.php.net/manual/en/control-structures.switch.php">case</a> '<span style="color: #8b0000">gif</span>':
 <a style="color: #ffa500" href="http://cn.php.net/manual/en/function.header.php">header</a>("<span style="color: #8b0000">Content-type: image/gif</span>");
 <a style="color: #0000ff" href="http://cn.php.net/break">break</a>;
<a style="color: #0000ff" href="http://cn.php.net/manual/en/control-structures.switch.php">case</a> '<span style="color: #8b0000">jpg</span>':
 <a style="color: #ffa500" href="http://cn.php.net/manual/en/function.header.php">header</a>("<span style="color: #8b0000">Content-type: image/jpeg</span>");
 <a style="color: #0000ff" href="http://cn.php.net/break">break</a>;
<a style="color: #0000ff" href="http://cn.php.net/manual/en/control-structures.switch.php">case</a> '<span style="color: #8b0000">png</span>':
 <a style="color: #ffa500" href="http://cn.php.net/manual/en/function.header.php">header</a>("<span style="color: #8b0000">Content-type: image/png</span>");
 <a style="color: #0000ff" href="http://cn.php.net/break">break</a>;
<a style="color: #0000ff" href="http://cn.php.net/manual-lookup.php?pattern=default&amp;lang=en&amp;scope=404quickref">default</a>:
 <a style="color: #ffa500" href="http://cn.php.net/manual/en/function.header.php">header</a>("<span style="color: #8b0000">Content-type: text/plain</span>");
}

<span style="color: #008000"># GZIP the content</span>
<a style="color: #0000ff" href="http://cn.php.net/manual/en/control-structures.if.php">if</a>(<a style="color: #ffa500" href="http://cn.php.net/extension_loaded">extension_loaded</a>('<span style="color: #8b0000">zlib</span>')){<a style="color: #ffa500" href="http://cn.php.net/ob_start">ob_start</a>();<a style="color: #ffa500" href="http://cn.php.net/ob_start">ob_start</a>('<span style="color: #8b0000">ob_gzhandler</span>');}

<span style="color: #008000"># echo the file's contents</span>
<a style="color: #0000ff" href="http://cn.php.net/echo">echo</a> <a style="color: #ffa500" href="http://cn.php.net/implode">implode</a>('<span style="color: #8b0000"></span>', <a style="color: #ffa500" href="http://cn.php.net/manual/en/function.file.php">file</a>($<a style="color: #ffa500" href="http://cn.php.net/manual/en/function.file.php">file</a>));

<a style="color: #0000ff" href="http://cn.php.net/manual/en/control-structures.if.php">if</a>(<a style="color: #ffa500" href="http://cn.php.net/extension_loaded">extension_loaded</a>('<span style="color: #8b0000">zlib</span>')){
<a style="color: #ffa500" href="http://cn.php.net/ob_end_flush">ob_end_flush</a>();
<span style="color: #008000"># set header the content's length;</span>
<span style="color: #008000"># header("Content-Length: ".ob_get_length()); # (It doesn't work? )</span>
<a style="color: #ffa500" href="http://cn.php.net/ob_end_flush">ob_end_flush</a>();
}
<span style="color: #0000ff">?&gt;</span></pre>
</blockquote>
<p>上面代码不仅压缩代码，而且要求浏览器端Cache这个文件。甚至，可以把所有css文件都放到一个php文件里，只要在echo处加入多个文件即可。</p>
<p>下面是一些常用的测试网站：</p>
<ul>
<li><a href="http://www.gidnetwork.com/tools/gzip-test.php" target="_blank">GZIP Test</a>: 测试一个URL地址是否支持GZIP传输，以及压缩比率。
</li>
<li><a href="http://www.websiteoptimization.com/services/analyze/index.html" target="_blank">Web Page Analyzer</a>：网站页面速度报告
</li>
<li><a href="http://www.ircache.net/cgi-bin/cacheability.py" target="_blank">Cacheability Test</a>: 测试一个页面是否可Cache</li>
</ul>
<p>参考资料：</p>
<ul>
<li><a href="http://www.mnot.net/cache_docs/" target="_blank">Caching Tutorial</a>
</li>
<li><a href="http://blog.htmlor.com/2006/08/03/serving_javascript_fast_chinese/">flickr对javascript干的好事</a>
</li>
<li><a href="http://www.badpenguin.org/php-howto-control-page-caching" target="_blank">Working with cached pages</a></li>
</ul>
<div><h4>相关文章</h4><ul><li><a href="http://zhiqiang.org/blog/it/innerhtml-2-cd-issue-dynamic-modification-table-function-of-js.html">innerHTML只读问题 &amp; 两个动态修改Table的js函数</a></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-set-browser-cache.html">加速blog：设置浏览器缓存</a></li><li><a href="http://zhiqiang.org/blog/it/speedup-website-with-css-sprites.html">用CSS Sprites让网站再加速</a></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-gzip-javascript-css.html">加速blog：GZIP压缩传输你的文件</a></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-about-javascript-css.html">加速blog：关于javascript(css)</a></li><li><a href="http://zhiqiang.org/blog/it/using-htaccess-to-speed-blog.html">设置blog的缓存和压缩</a></li><li><a href="http://zhiqiang.org/blog/it/to-get-gibberish-wordpress-move-to-dreamhost.html">搞定乱码，WordPress搬家到dreamhost</a></li><li><a href="http://zhiqiang.org/blog/it/dreamhost-system-load-average-load-recording-and-analysis.html">Dreamhost系统Load average负载记录和分析</a></li><li><a href="http://zhiqiang.org/blog/it/move-to-the-blog-server-bluehost.html">blog服务器搬家到bluehost</a></li></ul></div>    <p></p>
    <hr noshade style="margin:0;height:1px" />
    <p>&copy; zhiqiang for <a href="http://zhiqiang.org/blog">阅微堂</a>, 2007. | <a href="http://zhiqiang.org/blog/it/dreamhost-on-gzip-cache.html">&#38142;&#25509;</a> | <a href="http://zhiqiang.org/blog/it/dreamhost-on-gzip-cache.html#comments">18 &#26465;&#35780;&#35770;</a></p>]]></content:encoded>
			<wfw:commentRss>http://zhiqiang.org/blog/it/dreamhost-on-gzip-cache.html/feed</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
	</channel>
</rss>

