15

当我为SpeedTest测试我的网站时,我看到很多过期未指定错误。您可以在此页面上看到。

我将此代码添加到我的 .htaccess 文件中

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"
</IfModule>
## EXPIRES CACHING ##

但什么都没有改变。你能给我一个建议吗,我可以为我的静态文件指定到期日期吗?

服务器:Linux - 阿帕奇

4

5 回答 5

39

我喜欢 html5boilerplate 方式:

<IfModule mod_expires.c>
  ExpiresActive on

# Perhaps better to whitelist expires rules? Perhaps.
  ExpiresDefault                          "access plus 1 month"

# cache.appcache needs re-requests in FF 3.6 (thx Remy ~Introducing HTML5)
  ExpiresByType text/cache-manifest       "access plus 0 seconds"

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

# data
  ExpiresByType text/xml                  "access plus 0 seconds"
  ExpiresByType application/xml           "access plus 0 seconds"
  ExpiresByType application/json          "access plus 0 seconds"

# rss feed
  ExpiresByType application/rss+xml       "access plus 1 hour"

# favicon (cannot be renamed)
  ExpiresByType image/x-icon              "access plus 1 week" 

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

# htc files  (css3pie)
  ExpiresByType text/x-component          "access plus 1 month"

# webfonts
  ExpiresByType font/truetype             "access plus 1 month"
  ExpiresByType font/opentype             "access plus 1 month"
  ExpiresByType application/x-font-woff   "access plus 1 month"
  ExpiresByType image/svg+xml             "access plus 1 month"
  ExpiresByType application/vnd.ms-fontobject "access plus 1 month"

# css and javascript
  ExpiresByType text/css                  "access plus 2 months"
  ExpiresByType application/javascript    "access plus 2 months"
  ExpiresByType text/javascript           "access plus 2 months"

  <IfModule mod_headers.c>
    Header append Cache-Control "public"
  </IfModule>

</IfModule>

希望这对您有用。来源: https ://github.com/h5bp/html5-boilerplate/blob/master/dist/.htaccess

于 2011-04-13T19:20:02.240 回答
5

您可以使用 mod_headers 来解决问题:

<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
  Header set Cache-Control "max-age=290304000, public"
</FilesMatch>

或者你可以使用 mod_expires:

ExpiresByType text/html "access plus 1 month 15 days 2 hours"
ExpiresByType image/gif "modification plus 5 hours 3 minutes"
于 2011-04-13T18:02:06.297 回答
1

我看到您安装了 PHP(参考 wordpress),所以我们将尝试诊断您的问题:

  1. 重新启动您的 apache 网络服务器。如果你拥有 apache 服务器(命令行访问),你可以这样
  2. 创建一个 phpinfo 页面。这将允许您查看已安装的 apache 模块。检查您想要使用的 mod_expires.c 模块(应该能够在您的浏览器中为 mod_expires 执行 CTL-F(查找)请求)。如果你找到它 - 好!如果没有,您需要在您的 apache 服务器中安装 mod_expires。如果您拥有 apache 服务器,则可以搜索如何为您的特定操作系统安装模块。如果没有,您可以咨询您的托管服务提供商,了解如何安装此模块。安装后,继续。
  3. 修改您的 http.conf 条目以在您的表达式中包含“加号”
  4. 再次重启 Apache
  5. 测试 =)
于 2011-04-13T18:11:19.467 回答
1

您只需要添加:

ExpiresByType application/javascript "access plus 1 year"
于 2015-10-08T13:36:45.273 回答
0

也许你没有加载 mod_expires,所以整个节都是空操作。或者,AllowOverride 为 None 并且您的 .htaccess 文件未被处理。

于 2011-04-07T10:43:14.367 回答