.htaccess Gzip无法正常工作

时间:2012-07-13 16:23:40

标签: javascript apache .htaccess gzip

我正在尝试压缩一些文件以提高网站的速度。我在htaccess文件中执行此操作,除了几个文件之外,它正在工作。

以下是我用来压缩文件的代码:

<IfModule mod_gzip.c>
mod_gzip_on       Yes
mod_gzip_dechunk  Yes
mod_gzip_item_include file      \.(html?|txt|css|js|min|js?|js?1|1|css|php|pl|svg)$
mod_gzip_item_include handler   ^cgi-script$
mod_gzip_item_include mime      ^text/.*
mod_gzip_item_include mime      ^text/css.*
mod_gzip_item_include mime      ^application/javascript.*
mod_gzip_item_include mime      ^application/.*
mod_gzip_item_exclude mime      ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>

但是这4个文件没有被压缩,我看不出原因:

tryten . com/javascript/jquery.js?1 
tryten . com/javascript/jquery/plugins/jquery-ui.min.js
tryten . com/javascript/common.js?1 
tryten . com/javascript/jquery/plugins/imodal/imodal.js?1

1 个答案:

答案 0 :(得分:0)

试试这个

# Enable GZIP
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch bMSIE !no-gzip !gzip-only-text/html
</ifmodule>
# Expires Headers – 2678400s = 31 days
<ifmodule mod_expires.c>
  ExpiresActive On
  ExpiresDefault “access plus 1 seconds”
  ExpiresByType text/html “access plus 0 seconds”
  ExpiresByType image/gif “access plus 2678400 seconds”
  ExpiresByType image/jpeg “access plus 2678400 seconds”
  ExpiresByType image/png “access plus 2678400 seconds”
  ExpiresByType text/css “access plus 518400 seconds”
  ExpiresByType text/javascript “access plus 2678400 seconds”
  ExpiresByType application/x-javascript “access plus 2678400 seconds”
</ifmodule>
# Cache Headers
<ifmodule mod_headers.c>
  # Cache specified files for 31 days
  <filesmatch “.(ico|flv|jpg|jpeg|png|gif|css|swf)$”>
  Header set Cache-Control “max-age=2678400, public”
  </filesmatch>
  # Don’t cache HTML
  <filesmatch “.(html|htm)$”>
  Header set Cache-Control “max-age=0, private, must-revalidate”
  </filesmatch>
  # Cache PDFs for a day
  <filesmatch “.(pdf)$”>
  Header set Cache-Control “max-age=86400, public”
  </filesmatch>
  # Cache Javascripts for 31 days
  <filesmatch “.(js)$”>
  Header set Cache-Control “max-age=2678400, private”
  </filesmatch>
</ifmodule>

source