Search code examples
apache.htaccessmod-expires

Apache using htaccess for browser caching


I implemented the following to leverage browser caching in my htaccess (in the root)

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/javascript "access plus 1 month"
ExpiresByType application/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"

however I still get warnings about browser caching from gtmetrix (pagespeed and yslow).

Have I done something wrong here? GTMetrix reports my JS resources only have an expiry of 2 days (default).


Solution

  • Try using the header set for caching by setting the expire date in the future. You can use filematch for specific file types. A year typically a good time. Edit the file types as necessary.

        <filesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|swf|js|css)$">
           Header set Expires "Mon, 15 Apr 2015 20:00:01 GMT"
        </filesMatch>
    

    This definitely works for me.