【问题标题】:Caching and gzip compression by htaccess通过 htaccess 进行缓存和 gzip 压缩
【发布时间】:2011-10-07 03:53:28
【问题描述】:

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

除了我的访问者之外,我还试图让 Google PageSpeed 满意。 我想通过 .htaccess 使用缓存和 gzip 压缩,请帮我写代码!

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

我想缓存 xml,txt 文件 2 天

我想缓存 html 文件 2 天

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

有没有办法使用 .htaccess 压缩图像?

【问题讨论】:

  • 使用gzip 压缩图像并没有多大意义,因为 GIF、JPEG 或 PNG 已经是压缩格式。压缩它们只会损失服务器上的 CPU 资源,而不会增加带宽。
  • @hakre: 除了这个家伙,我没有正确的答案!
  • 你是@Hamid Seyyedi 吗?如果是,则此用户帐户和your other one 需要合并为一个帐户。您不能拥有两个单独的帐户。

标签: .htaccess caching gzip


【解决方案1】:

通过 .htaccess 启用压缩

对于大多数阅读本文的人来说,通过将一些代码添加到其 Web 主机/服务器上名为 .htaccess 的文件中来启用压缩。这意味着转到您的虚拟主机上的文件管理器(或您添加或上传文件的任何地方)。

.htaccess 文件控制着您网站的许多重要事项。如果您不熟悉 .htaccess 文件,请阅读我的使用 .htaccess 文章以了解如何在更改之前了解如何操作。

以下代码应添加到您的 .htaccess 文件中...

<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 compression tool 检查您的压缩是否正常工作。

【讨论】:

    【解决方案2】:

    你应该在你的 .htaccess 文件中使用以下规则,相信我,如果你还没有使用它,它会增加至少 10 分。

    mod_gzip_on 是 mod_gzip_dechunk 是 mod_gzip_item_include 文件 .(html?|txt|css|js|php|pl)$ mod_gzip_item_include 处理程序 ^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.* AddOutputFilterByType DEFLATE 应用程序/javascript AddOutputFilterByType DEFLATE application/rss+xml AddOutputFilterByType DEFLATE application/vnd.ms-fontobject AddOutputFilterByType DEFLATE application/x-font AddOutputFilterByType DEFLATE application/x-font-opentype AddOutputFilterByType DEFLATE application/x-font-otf AddOutputFilterByType DEFLATE application/x-font-truetype AddOutputFilterByType DEFLATE application/x-font-ttf AddOutputFilterByType DEFLATE 应用程序/x-javascript AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE 应用程序/xml AddOutputFilterByType DEFLATE 字体/opentype AddOutputFilterByType DEFLATE 字体/otf AddOutputFilterByType DEFLATE 字体/ttf AddOutputFilterByType DEFLATE image/svg+xml AddOutputFilterByType DEFLATE image/x-icon AddOutputFilterByType DEFLATE 文本/css AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE 文本/javascript AddOutputFilterByType DEFLATE 文本/纯文本 AddOutputFilterByType DEFLATE 文本/xml BrowserMatch ^Mozilla/4 gzip-only-text/html BrowserMatch ^Mozilla/4\.0[678] 无 gzip BrowserMatch \bMSIE !no-gzip !gzip-only-text/html 标头追加不同的用户代理 过期活动在 ExpiresByType image/jpg "访问 1 年" ExpiresByType image/jpeg "访问 1 年" ExpiresByType image/gif "访问 1 年" ExpiresByType image/png "访问 1 年" ExpiresByType text/css "访问 1 个月" ExpiresByType text/html "访问 1 个月" ExpiresByType 申请/pdf “访问 1 个月” ExpiresByType text/x-javascript "访问 1 个月" ExpiresByType application/x-shockwave-flash "访问 1 个月" ExpiresByType image/x-icon "访问 1 年" ExpiresDefault "访问 1 个月"

    【讨论】:

      【解决方案3】:

      试试这个:

      <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>
      

      【讨论】:

      • 在 apache 2.4 中必须启用这两个模块才能使用 deflating: mod_deflate.so 和 mod_filter.so
      【解决方案4】:

      您可以将代码用作:-

      <Files *>
          #Compress
      
          SetOutputFilter GZIP
      
          #Caching
      
          Header set Cache-Control "max-age=3600, public"
      
      
      </Files>
      

      【讨论】:

        【解决方案5】:
        # 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>
        

        【讨论】:

        • @hakre:除了这个家伙,我没有正确的答案!睁大你的眼睛!
        • 部分对我不起作用。当我在自己的网站上尝试时,我得到了错误 500!但是,下面答案中的 AddOutputFilterByType 方法效果很好。这两种方法有什么区别?
        • Apache 1.3 使用 mod_gzip 而 Apache 2.x 使用 mod_deflate。所以请检查你的 apache 版本,它很重要!
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-09-27
        • 2013-08-18
        • 2011-08-16
        • 1970-01-01
        • 2010-11-12
        • 2012-12-09
        • 1970-01-01
        相关资源
        最近更新 更多