htaccess 缓存和 gzip 压缩 [英] Caching and gzip compression by htaccess
问题描述
有人可以为我提供一个优化的 .htaccess 配置来处理典型网站的压缩、浏览器缓存、代理缓存等吗?
Can someone provide me with an optimized .htaccess configuration that handles compression, browser caching, proxy caching, etc. for a typical website?
除了我的访问者之外,我还努力让 Google PageSpeed 感到高兴.我想通过 .htaccess 使用缓存和 gzip 压缩,请帮我写代码!
Aside from my visitors, I'm also trying to make Google PageSpeed happy. I wanna use caching and gzip compression through .htaccess please help me with its code!
我想长时间缓存icon,pdf,flv,jpg,png,gif,js,css,swf
文件
我想缓存 xml,txt
文件 2 天
I want to cache xml,txt
files for 2 Days
我想缓存 html
文件 2 天
I want to cache html
files for 2 Days
我想压缩我的 html,txt,css,js,php
文件,因为这些文件很大.
And I wanna compress my html,txt,css,js,php
files because those have large file size.
有没有办法使用 .htaccess 对图像进行 gzip 压缩?
Is there any way to gzip images using .htaccess?
推荐答案
# 480 weeks
<FilesMatch ".(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Cache-Control "max-age=290304000, public"
</FilesMatch>
# 2 DAYS
<FilesMatch ".(xml|txt)$">
Header set Cache-Control "max-age=172800, public, must-revalidate"
</FilesMatch>
# 2 HOURS
<FilesMatch ".(html|htm)$">
Header set Cache-Control "max-age=7200, must-revalidate"
</FilesMatch>
<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>
这篇关于htaccess 缓存和 gzip 压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!