How To Set Expires / Cache-Control Headers For All Files On Your Website

Setting up caching for your static files is a quick and easy way to greatly reduce your page load time. It’s great for user experience, and will also get you a small SEO boost.

Its as easy as putting one line of code in your .htaccess file. Google likes to see the expires headers to be set at least one week in the future, so this first code snippet sets it for just over a week (7.1 days)

Header set Cache-Control "max-age=614000, public, must-revalidate"

You can change the number in the middle of the line from “614000” to anything you like. The number represents seconds, and there are “86400” seconds in one day. So if you wanted to set the Cache Control for 30 days, you would just multiply it by 30 to get this line of code:

Header set Cache-Control "max-age= 2592000, public, must-revalidate"

You should always double check to make sure that its working once you make this switch. You can do this by going to Google’s Page Speed Insights. While you’re there, you’ll be able to see all of Google’s recommendations for your site.

If they mention that you should gzip your files for delivery, check out my guide on How To Deliver Files With gzip On Apache Servers

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>