Gzip压缩和缓存过期无效

时间:2013-08-16 09:19:37

标签: .htaccess xampp gzip cache-expiration

我已在.htaccess文件中配置了缓存过期和gzip压缩,但根据yslow chrome扩展统计信息无法正常工作。

这是我的 .htaccess

# ----------------------------------------------------------------------
# Mod Rewrite
# ----------------------------------------------------------------------

<IfModule mod_rewrite.c>
    Options -MultiViews
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

# ----------------------------------------------------------------------
# gZip Compression
# ----------------------------------------------------------------------

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

# ----------------------------------------------------------------------
# Expires headers (for better cache control)
# ----------------------------------------------------------------------

<IfModule mod_expires.c>
  ExpiresActive on

# Your document html
  ExpiresByType text/html "access plus 0 seconds"

# Media: images, video, audio
  ExpiresByType audio/ogg "access plus 1 month"
  ExpiresByType image/gif "access plus 1 month"
  ExpiresByType image/jpeg "access plus 1 month"
  ExpiresByType image/png "access plus 1 month"
  ExpiresByType video/mp4 "access plus 1 month"
  ExpiresByType video/ogg "access plus 1 month"
  ExpiresByType video/webm "access plus 1 month"

# CSS and JavaScript
  ExpiresByType application/javascript "access plus 1 year"
  ExpiresByType text/css "access plus 1 year"
</IfModule>

注意:我正在使用Xampp

1 个答案:

答案 0 :(得分:2)

这是我在这里的第一篇文章!

缓存过期:

您是否检查过apache / conf / httpd.conf中是否启用了模块“mod_expires.so”? 搜索专栏#LoadModule expires_module modules/mod_expires.so并删除“#”符号。之后它应该有效。

对于gzip压缩:

我使用“mod_deflate”来启用压缩。我还使用“mod_mime”,“mod_setenvif”和“mod_headers”。检查每个都在httpd.conf中启用。这种组合对我有用。

Htaccess我的压缩:

<IfModule mod_mime.c>
 AddType application/x-javascript .js
 AddType text/css .css
</IfModule>

<IfModule mod_deflate.c>
 SetOutputFilter DEFLATE
 <IfModule mod_setenvif.c>
  SetEnvIfNoCase Request_URI \.(?:rar|zip)$ no-gzip dont-vary
  SetEnvIfNoCase Request_URI \.(?:gif|jpg|png)$ no-gzip dont-vary
  SetEnvIfNoCase Request_URI \.(?:avi|mov|mp4)$ no-gzip dont-vary
  SetEnvIfNoCase Request_URI \.mp3$ no-gzip dont-vary
 </IfModule>
 <IfModule mod_setenvif.c>
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
 </IfModule>
 <IfModule mod_headers.c>
  Header append Vary User-Agent env=!dont-vary
 </IfModule>
</IfModule>

更多信息here。我希望有所帮助。

相关问题