本站之前写过一篇有关WordPress 使用 W3 total cache 结合 MaxCDN 为WordPress站点全面提速的文章:W3 total cache 结合 Max CDN 为您的WordPress站点全面提速,后来经测试感觉 W3 total cache 太过于消耗服务器资源,于是最后选择了较为轻快很多的 wp super cache,不过wp super cache 对于浏览器缓存支持得不够好,尽管插件设置上边有提示,采用PHP缓存替代Mod rewrite缓存的话是可以开启浏览器缓存支持的,但是经过本人的测试,还是未能成功,于是彻底放弃了从wp super cache插件本身来实现Leverage browser caching的想法。
于是想到了通过apache的mod_expires模块通过 .htaccess来实现Leverage browser caching,经过一番Google搜索比较,发现以下代码支持得堪称完美,共享给大家:
# Turn on Expires and set default to 0 ExpiresActive On ExpiresDefault A0 # Set up caching on media files for 1 year (forever?) <FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$"> ExpiresDefault A29030400 Header append Cache-Control "public" </FilesMatch> # Set up caching on media files for 1 week <FilesMatch "\.(gif|jpg|jpeg|png|swf)$"> ExpiresDefault A604800 Header append Cache-Control "public" </FilesMatch> # Set up 2 Hour caching on commonly updated files <FilesMatch "\.(xml|txt|html|js|css)$"> ExpiresDefault A7200 Header append Cache-Control "proxy-revalidate" </FilesMatch> # Force no caching for dynamic files <FilesMatch "\.(php|cgi|pl|htm)$"> ExpiresActive Off Header set Cache-Control "private, no-cache, no-store, proxy-revalidate, no-transform" Header set Pragma "no-cache" </FilesMatch>
通过开启对Leverage browser caching的支持以后,本站在Google page speed中测试重新获得了较高的分数。