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

Posted in Notes on 22 February 2016

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.

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

 

Related Notes Posts

February 2024

Animated SVGs (Scalable Vector Graphics)

Animated SVGs, Scalable Vector Graphics, are increasingly popular choices for adding dynamic elements to websites. Their scalability, lightweight nature, and flexibility make them attractive options... Continue reading

December 2023

Using Scalable Vector Graphics (SVGs)

Scalable Vector Graphics (SVGs) have revolutionised website design, offering unparalleled flexibility, scalability, and interactivity. As versatile graphic elements, SVGs can enhance the visual appeal and... Continue reading

September 2023

Designing websites for accessibility

In the ever-evolving landscape of web design, the balancing act between accessibility and aesthetic appeal remains a crucial consideration. As the digital realm becomes increasingly... Continue reading

More Notes Posts