.htaccess: How to “Specify a cache validator”?

后端 未结 3 2059
醉话见心
醉话见心 2020-12-24 07:18

I\'m running Google PageSpeed on my site and it\'s tell me that I need to
\"Specify a cache validator.\"

The following resources are missing a cache validator. R

相关标签:
3条回答
  • 2020-12-24 07:46

    I tested all of above codes, But see no change in gtmetrix rank. Using this improved Cache-Control (Specify a cache validator) rank for my wordpress site:

    ## 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 plus 1 year"
    </IfModule>
    ## EXPIRES CACHING ##
    
    <ifModule mod_headers.c>
      <filesMatch "\\.(ico|pdf|flv|jpg|jpeg|png|gif|swf)$">
        Header set Cache-Control "max-age=2592000, public"
      </filesMatch>
    
      <filesMatch "\\.(css)$">
        Header set Cache-Control "max-age=2592000, public"
      </filesMatch>
    
      <filesMatch "\\.(js)$">
        Header set Cache-Control "max-age=216000, private"
      </filesMatch>
    
      <filesMatch "\\.(xml|txt)$">
        Header set Cache-Control "max-age=216000, public, must-revalidate"
      </filesMatch>
    
      <filesMatch "\\.(html|htm|php)$">
        Header set Cache-Control "max-age=1, private, must-revalidate"
      </filesMatch>
    </ifModule>
    

    I recommend you to customize max-age values for your site and it's files yourself.

    0 讨论(0)
  • 2020-12-24 07:58

    In order to "Set A Cache Validator" you need to send the following in your headers:

    Expires or Cache-Control: max-age

    AND

    Last-Modified or ETag

    So, for example, in PHP you could add the following for CSS and JS files:

    <filesMatch "\.(js|css)$">
        Header set Expires "Thu, 21 May 2013 20:00:00 GMT"
        Header set Last-Modified "Thu, 21 May 2012 20:00:00 GMT"
    </filesMatch>
    

    This will satisfy Google's Pagespeed calculator.

    0 讨论(0)
  • 2020-12-24 07:59

    I think the problem you are having is with Expire: and not with Last-Modified:. Apache would by default send the file Last-Modified: header based on the file date. I suggest removing the upper code and replacing it with the following:

    <IfModule mod_expires.c>
        ExpiresActive On
        ExpiresDefault "access plus 1 year"
    </IfModule>
    

    Try with that, if it didn't work try adding this as well:

    <IfModule mod_headers.c>
        <FilesMatch "\.(bmp|css|flv|gif|ico|jpg|jpeg|js|pdf|png|svg|swf|tif|tiff)$">
            Header set Last-Modified "Mon, 31 Aug 2009 00:00:00 GMT"
        </FilesMatch>
    </IfModule>
    
    0 讨论(0)
提交回复
热议问题