【问题标题】:Apache modules installed but htaccess caching doesn't work已安装 Apache 模块但 htaccess 缓存不起作用
【发布时间】:2013-01-19 21:10:33
【问题描述】:

我正在尝试通过我的 VPS 服务器上的 .htaccess 规则设置缓存。我确保安装了 mod_expires 和 mime 类型检测,但是当我通过 gtmetrix 运行我的网站时,它显示我没有启用缓存。这是我的 .htaccess 规则:

# Expire headers
<ifModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault "access plus 1 seconds"
  ExpiresByType image/x-icon "access plus 2592000 seconds"
  ExpiresByType image/jpeg "access plus 2592000 seconds"
  ExpiresByType image/png "access plus 2592000 seconds"
  ExpiresByType image/gif "access plus 2592000 seconds"
  ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"
  ExpiresByType text/css "access plus 604800 seconds"
  ExpiresByType text/javascript "access plus 216000 seconds"
  ExpiresByType application/javascript "access plus 216000 seconds"
  ExpiresByType application/x-javascript "access plus 216000 seconds"
  ExpiresByType text/html "access plus 600 seconds"
  ExpiresByType application/xhtml+xml "access plus 600 seconds"
</ifModule>

# Cache-Control Headers
<ifModule mod_headers.c>
#month
  <filesMatch "\.(ico|jpe?g|png|gif|swf)$">
    Header set Cache-Control "max-age=2592000, public"
  </filesMatch>
#week
  <filesMatch "\.(css|js)$">
    Header set Cache-Control "max-age=604800, public"
  </filesMatch>
#day
  <filesMatch "\.(x?html?|php)$">
    Header set Cache-Control "max-age=43200, private, must-revalidate"
  </filesMatch>
</ifModule>
# END Cache-Control Headers

# Turn ETags Off
<ifModule mod_headers.c>
  Header unset ETag
</ifModule>
FileETag None

# Remove Last-Modified Header
<ifModule mod_headers.c>
  Header unset Last-Modified
</ifModule>

【问题讨论】:

    标签: apache .htaccess vps


    【解决方案1】:

    尝试在 FilesMatch 块中添加 Last-Modifed。 像这样的:

    <FilesMatch "\.(jpg|jpeg|png|gif|js|css|ico)$">
      ExpiresDefault "access plus 10 years"
      Header  set Cache-Control "public"
      Header  set Last-Modified "Mon, 31 Aug 2009 00:00:00 GMT"
      Header  unset ETag 
      Header  append Cache-Control "max-age=604800, public"
    </FilesMatch >
    

    【讨论】:

    • 我添加了它,但仍然没有运气。
    • 确保 mod_headers.c 已启用?尝试删除 块。在这种情况下,如果模块 mod_headers.c 未启用,您将收到错误消息。
    • 好的,我删除了 mod_headers.c 块并且没有收到错误。
    • 要排除 htaccess 的覆盖问题,您可以尝试将所有这些从 htaccess 文件移动到 httpd.conf 文件并重新启动 Apache 服务器吗?
    • 好的,我做到了,但它仍然无法正常工作。 Chrome 的页面速度插件仍显示(1 天)缓存。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-13
    • 1970-01-01
    • 1970-01-01
    • 2019-05-20
    • 1970-01-01
    • 1970-01-01
    • 2022-10-08
    相关资源
    最近更新 更多