不能使用wordpress和.htacces来利用缓存浏览器

时间:2017-01-17 15:04:55

标签: php wordpress apache caching

我的wordpress .htacces

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

然而,它不起作用,我仍然在Google Pagespeed中有很多缓存错误。

2 个答案:

答案 0 :(得分:1)

你的语法很好。如果它不起作用,则无法启用mod_expires。

  • 启用mod_expires:a2enmod expires
  • 重启Apache:/etc/init.d/apache2 restart

答案 1 :(得分:0)

试试这个:

# Enable browser caching for most static assets, 1 month
<filesMatch ".(css|jpg|jpeg|png|gif|pdf|js|ico|svg|ttf|woff|woff2)$">
Header set Cache-Control "max-age=2592000, public"
</filesMatch>

max-age更改为您想要的秒数。我的设定为1个月。

这使用缓存控制而不是到期。缓存控制是更现代的解决方案。这里有一个很好的概述差异以及如何使用它们: http://dev.mobify.com/blog/beginners-guide-to-http-cache-headers/

相关问题