Caution: This post was published in 2016 and may contain information, techniques, or code examples that are no longer current. Please double-check official documentation and modern best practices before using anything from this article.

Useful .htaccess Rules: Setting the expires header for browser caching

Expires headers let the browser know whether to serve a cached version of the page. This can simultaneously help to reduce server load and increase page load time by telling the browser that it doesn't have to check for new versions of files for an extended period.

The code below will allow images to be cached for a year and text-based content to be cached for one month. For most fairly static websites (like this one) that doesn't change very often aside for a new post now and then this should be more then enough.

If you are running a dynamic website be careful with any sensitive information which may be stored, for example JSON responses, user account pages and the like should be set-up to not cache.

## START EXPIRES CACHING ##
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 plus 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"

END EXPIRES CACHING

The above can be copied into your .htaccess file without any major issues but, of course best checked before deploying to a live environment.

 

Let’s build something great together

I’m Keith, a web developer based in Belfast. From fast, efficient PHP builds to Shopify stores and secure WordPress or Magento setups, I help businesses create websites that perform and grow.

Let’s talk about your next project

Recent posts

Using WordPress as a static site generator

Static site generators have gained significant traction amongst developers, designers, and businesses seeking faster, more secure websites. Unlike traditional dynamic sites, which rely on a database to deliver content on…

Continue reading

Simple steps to protect your privacy online

In today’s digital world, protecting your privacy online has become essential. With personal data constantly being shared, stored, and potentially accessed by unauthorised parties, safeguarding your privacy can help you…

Continue reading

Moving a WordPress Website with ACF and Custom Post Types to Brightspot CMS

Migrating a website from WordPress to Brightspot CMS can seem daunting, particularly when the WordPress installation relies heavily on Advanced Custom Fields and Custom Post Types . Both ACF amd…

Continue reading

Switching to Eleventy

When I first set up my personal website it was powered by WordPress. It did the job for a while, but I soon found myself frustrated by the overhead and…

Continue reading