设置blog的缓存和压缩

在网站根目录下的.htaccess文件中添加以下代码

<IfModule mod_expires.c>
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
</IfModule>

<IfModule mod_deflate.c>
<FilesMatch "\.(js|css|html|htm)$">
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>

<ifModule mod_gzip.c>
  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.*
</ifModule>

<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css)$">
Header unset Last-Modified
</FilesMatch>

FileETag none

现在使用Firefox的YSlow插件,以Small Site or Blog标准测试的分数为99分,以YSlow2(V2)标准测试的分数为94分,被扣掉的几分主要是因为没有使用CDN。所以现在速度已经被优化到了极致

赶快把它放到你的网站或blog根目录下的.htaccess文件里吧。

写于2009年十二月 8日

17条留言 -> 跳到留言表格
  • At 2009.12.09 08:51, 人来人往 said:

    不懂……能否解释下原理呀 :?

    • At 2009.12.09 09:01, 人来人往 said:

      在以前的文章里找到了 ,谢谢志强兄 :-)

    • At 2009.12.09 09:19, 无限 said:
      • At 2009.12.09 12:42, zhiqiang said:

        看了你说的东西了,很有趣 :-D ,可备用来害人

        但是文中所用的方法是无害的,不要误导大家~

      • At 2009.12.10 12:18, 11 said:

        这个借用一下下载http://tinybb.ekedns.com/test/1k.php
        :D

        • At 2009.12.10 17:09, 我的YSlow得分 @ agon - 破地儿 said:

          [...] 刚在阅微堂看到zhiqiang可以到90几分。由于自己用的是国外免费空间,性能一般,所以当初就是受他的启发,对自己的博客进行前台优化。后来又在askapache上看到很多关于.htaccess使用设置方法。我自己最后也弄出一个各种情况下的完美gzip压缩解决办法,总结了一些cache优化设置。 [...]

          • At 2009.12.10 17:21, agon said:

            从读你那个系列开始受启发,又经过好长时间,再换个个主题,偶也要90多了

            • At 2009.12.12 12:17, 设置blog的缓存和压缩 « 明教责任田 said:

              [...] 设置blog的缓存和压缩 于 09-12-8 通过 阅微堂 作者:zhiqiang [...]

              • At 2009.12.17 04:07, SUN said:

                其实可以把gif和jpeg也加上(不少用户可能懒得或者没有对图进行优化),这样的话还能更省一些。

                • At 2009.12.20 21:58, daniel said:

                  blog越快越好,赶紧去试试,3Q

                  • At 2009.12.21 23:24, 翎峋 said:

                    也可以对php、html压缩哦,这岂不是整个网站都压缩了?

                    • At 2009.12.23 22:55, 外贸主机 said:

                      老大,这个代码能不能自动释放缓存啊?

                      • At 2009.12.24 09:54, zhiqiang said:

                        不知道你说的自动释放缓存是什么意思~

                        如果使用上面的代码,浏览器端的缓存是服务器无法自动清除的,这是为了最大化优化速度,不需要浏览器端访问服务器询问文件是否修改。

                        如果非要自动释放缓存,可通过更改url来实现,比如引用的css文件后面添加版本好,比如这样的: style.css?version=20091223

                      • At 2010.01.05 18:02, 小邓BLOG » 博客装修五 said:

                        [...] 参考:http://zhiqiang.org/blog/it/using-htaccess-to-speed-blog.html [...]

                        • At 2010.01.10 13:26, SUN said:
                          • At 2010.10.17 15:23, 向晚 said:

                            太厲害了,我博客一下從70+到90+了。 :-D

                            • At 2011.05.25 18:42, 我的YSlow得分 @ agon-小地儿 said:

                              [...] 刚在阅微堂看到zhiqiang可以到90几分。由于自己用的是国外免费空间,性能一般,所以当初就是受他的启发,对自己的博客进行前台优化。后来又在askapache上看到很多关于.htaccess使用设置方法。我自己最后也弄出一个各种情况下的完美gzip压缩解决办法,总结了一些cache优化设置。 [...]

                              (Required)
                              (Required, not published)

                                B | I | U | D | 添加链接 | 插入引用 | 插入代码 | 插入表情 | | + | ?

                              guest | 注册 | 管理

                              阅微堂

                              理工科背景的证券从业人员
                              Loading...
                              Loading...
                              Loading...