.htaccesscachingbrowsermod-expires

How to use leverage caching on a website where ONLY FEW pages need to updated frequently?


I wrote the expiry of png,jpg,html,css,js and all in the .htaccess as 1 month. But few of the html are being updated frequently. How can i manage that ? Also, if i change my .htaccess, will the browser download contents again or still load it from the cache ?

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

This is the code I wrote in .htaccess.

Now if I make any changes, will the browser still load from the cache because the expiry is 1month, or will it load the new content ?

In my case it loads the new content ONLY WHEN I HARD REFRESH A PAGE. But a user doesn't know that he needs to do that. What is the solution for this?


Solution

  • I would recommend that you set the expiry header based on the modification date of the source files. This can be done by:

    ExpiresActive On
    ExpiresDefault "modification plus 1 month"
    

    You could also achieve this using:

    <FilesMatch "\.(png|jpe?g|html?|css|js)$">
        ExpiresActive On
        ExpiresDefault "modification plus 1 month"
    </FilesMatch>