WordPress How To Set Expiry Dates For Images, Files Without A Plugin


About The Author
Avatar Cabe Nolan is the founder of WP Cover where he shares his insight into WordPress, development, & entrepreneurship. Outside of WPCover, Cabe continues to run a successful WordPress development firm, Bold City Design as well as a few high profile websites, Arrivala, Two Way Resume, Dock Skipper, and a successful outdoors brand, DolfinPack.







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:

# Expired
<IfModule mod_expires.c>
<Filesmatch "\.(jp?eg|png|gif|ico|woff)$">
    ExpiresActive on
    ExpiresDefault "access 1 year"
</Filesmatch>
<Filesmatch "\.(css|js|swf|mov|mp3|mpeg|mp4|ogg|ogv|ttf|xml|svg|html)$">
    ExpiresActive on
    ExpiresDefault "access 1 month"
</Filesmatch>
    ExpiresDefault "access 2 days"
</IfModule>
## 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.


Comments:

Share Your Thoughts

Leave a Reply

Your email address will not be published. Required fields are marked *


Related Stuff You Might Like







WordPress News, Tips, & Code Snippets

Join the WP Cover mailing list and get wordpress news, tips, code snippets, security warnings, and more delivered right to your inbox.  We won't flood your inbox, newsletters typically go out every 1-2 weeks unless it involves an important security release.

You have Successfully Subscribed!