【问题标题】:Google Speed Leverage browser caching谷歌速度利用浏览器缓存
【发布时间】:2011-07-22 17:50:02
【问题描述】:

我从 Google Speed 测试中得到了这个错误:

以下可缓存资源的新鲜度生命周期较短。为以下资源指定至少一周后的到期时间:

http://localhost/english/favicon.ico (expiration not specified)
http://localhost/english/images/bg_center.png (expiration not specified)
http://localhost/english/images/bg_top.jpeg (expiration not specified)
http://localhost/english/images/footer_bg2.png (expiration not specified)
http://localhost/english/images/m_facebook.png (expiration not specified)
http://localhost/english/images/m_rss.png (expiration not specified)
http://localhost/english/images/top_bg.png (expiration not specified)
http://localhost/english/javascript/gram.js (expiration not specified)
http://localhost/english/javascript/top_start.js (expiration not specified)
http://localhost/english/jquery.js (expiration not specified)
http://localhost/english/style/gram.css (expiration not specified)
http://localhost/english/style/style.css (expiration not specified)

我应该在我的 htaccess 文件中做些什么吗?

【问题讨论】:

    标签: php .htaccess caching browser-cache


    【解决方案1】:

    看起来静态文件没有设置过期时间。阅读 - http://www.absolutelytech.com/2010/08/02/howto-add-expire-headers-to-cache-static-files-using-htaccess/

    您需要在 .htaccess 中发布以下代码

    # Turn on the Expires engine
    ExpiresActive On
    
    # Expires after a month client accesses the file
    ExpiresByType image/jpeg A2592000
    ExpiresByType image/gif A2592000
    ExpiresByType image/png A2592000
    ExpiresByType image/x-icon A2592000
    ExpiresByType text/plain A2592000
    
    # Good for one week
    ExpiresByType application/x-javascript M604800
    ExpiresByType text/css M604800
    ExpiresByType text/html M604800
    

    【讨论】:

    • 如何使用 web.config 文件? (在 Windows 中),这可以在共享主机中完成吗?
    • 我对windows系统不太了解。如果它是托管服务提供商没有限制的目录特定配置,它也应该在共享托管环境中工作。
    【解决方案2】:

    PageSpeed:利用浏览器缓存 每次浏览器加载网页时,它都必须下载所有 Web 文件才能正确显示该页面。这包括所有的 HTML、CSS、javascript 和图像。

    要启用浏览器缓存,您需要编辑 HTTP 标头以设置某些类型文件的到期日期。

    在您的域的根目录中找到您的 .htaccess 文件,该文件是一个隐藏文件,但应该会显示在 FileZilla 或 CORE 等 FTP 客户端中。您可以使用记事本或任何形式的基本文本编辑器编辑 htaccess 文件。

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

    【讨论】:

      猜你喜欢
      • 2014-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-15
      • 2011-03-23
      相关资源
      最近更新 更多