首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过htaccess进行缓存和gzip压缩

通过htaccess进行缓存和gzip压缩
EN

Stack Overflow用户
提问于 2011-07-17 22:40:38
回答 4查看 69K关注 0票数 33

谁能为我提供一个优化的.htaccess配置,为一个典型的网站处理压缩,浏览器缓存,代理缓存等?

除了我的访问者,我也在努力让Google PageSpeed开心。我想通过.htaccess使用缓存和gzip压缩,请帮助我它的代码!

我想缓存很长时间的icon,pdf,flv,jpg,png,gif,js,css,swf文件

我想缓存2天的xml,txt文件

我想缓存2天的html文件

我想压缩我的html,txt,css,js,php文件,因为它们的文件很大。

有没有办法用.htaccess压缩图片?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-07-17 22:41:50

代码语言:javascript
复制
# 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>
票数 48
EN

Stack Overflow用户

发布于 2012-11-16 16:47:47

试试这个:

代码语言:javascript
复制
<IfModule mod_deflate.c>
    #The following line is enough for .js and .css
    AddOutputFilter DEFLATE js css
    AddOutputFilterByType DEFLATE text/plain text/xml application/xhtml+xml text/css   application/xml application/rss+xml application/atom_xml application/x-javascript application/x-httpd-php application/x-httpd-fastphp text/html

    #The following lines are to avoid bugs with some browsers
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4\.0[678] no-gzip
    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</IfModule>

# BEGIN EXPIRES
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 10 days"
    ExpiresByType text/css "access plus 1 week"
    ExpiresByType text/plain "access plus 1 month"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType application/x-javascript "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 week"
    ExpiresByType application/x-icon "access plus 1 year"
</IfModule>
# END EXPIRES

<IfModule mod_headers.c>
    <FilesMatch "\.(js|css|xml|gz)$">
        Header append Vary Accept-Encoding
    </FilesMatch>
    <FilesMatch "\.(ico|jpe?g|png|gif|swf)$">
        Header set Cache-Control "public"
    </FilesMatch>
    <FilesMatch "\.(css)$">
        Header set Cache-Control "public"
    </FilesMatch>
    <FilesMatch "\.(js)$">
        Header set Cache-Control "private"
    </FilesMatch>
    <FilesMatch "\.(x?html?|php)$">
        Header set Cache-Control "private, must-revalidate"
    </FilesMatch>
</IfModule>
票数 21
EN

Stack Overflow用户

发布于 2013-04-23 19:05:33

您可以使用以下代码:-

代码语言:javascript
复制
<Files *>
    #Compress

    SetOutputFilter GZIP

    #Caching

    Header set Cache-Control "max-age=3600, public"


</Files>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6724549

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档