WordPress How To Set Expiry Dates For Images, Files Without A Plugin
We’re going to continue on with our post from a couple days ago as we continue to improve search engine optimization (SEO) and better our Google Page Speed Insights test. Today, we’re going to look at how to set an expiry date or a maximum age in the HTTP headers for static resources. For many, this is going to be a red flag when you run the Google Speed Test on your site. Fortunately the fix is simple as it just requires adding some lines to your sites .htaccess file. Your .htaccess file is typically found in the public_html (or equivalent) folder.
Once you find the file, open it up and add the following lines to the top of the file:
ExpiresDefault "access 1 year"
ExpiresDefault "access 1 month"
ExpiresDefault "access 2 days"
## END EXPIRES ##
The above code tells browsers that media (such as jpg, png, ico, etc.) should be cached for a period of one year from the users access date. The next section tells browsers that other assets such as CSS, JS, etc. should be cached for a period of one month from the users access date. The final section tells browsers that any file types not outlined in the first two sections should be cached for a period of two days from the access date. Overall, pretty straight forward and easy to follow. Save the .htaccess file and you’re all set!
While the above is a very easy solution to implement, I would personally recommend using a CDN service such as Cloudflare. Cloudflare allows you to achieve this exact same concept by simply selecting a Browser Cache Expiration via a drop down menu. Instructions on Cloudflare’s website can be found here.