htaccess缓存和gzip压缩

Ham*_*edi 30 .htaccess caching gzip

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

除了我的访问者,我还试图让Google PageSpeed感到高兴.我想通过.htaccess使用缓存和gzip压缩,请帮助我使用它的代码!

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

我想缓存xml,txt2天的文件

我想缓存html2天的文件

我想压缩我的html,txt,css,js,php文件,因为那些文件很大.

有没有办法使用.htaccess gzip图像?

Ham*_*edi 47

# 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>
Run Code Online (Sandbox Code Playgroud)

  • <ifmodule>部分对我不起作用.当我在自己的网站上尝试时,我得到了错误500!但是,下面的答案中的AddOutputFilterByType方法非常有效.这两种方法有什么区别? (2认同)
  • Apache 1.3使用[mod_gzip](http://sourceforge.net/projects/mod-gzip/),而Apache 2.x使用[mod_deflate](http://httpd.apache.org/docs/2.0/mod/mod_deflate。 html)。因此,请检查您的apache版本,它很重要! (2认同)

小智 17

试试这个:

<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>
Run Code Online (Sandbox Code Playgroud)

  • 在apache 2.4中,必须启用这两个模块才能使用`deflating`:mod_deflate.so和mod_filter.so (2认同)