WordPress Solve Expires Headers Not Working

wordpress speed and optimization

A common issue you might see in Page Speed Insights or GTmetrix is to add expires headers for your content but what does this mean? Expires headers are a fancy way of the server saying to a browser you should save these files in your cache for x amount of time. The browser will then respond with ok and it will save them to its cache.

This system has been around for a very long time and many websites simply don’t make use of it or they are not configuring it properly which is causing more problems.

The reason it exists is to reduce the load time on your next page visit. The reason the load time is reduced is because the browser will not have to redownload the saved assets it will just load them from disk and thus reduce the amount of requests made to your web server. The idea is actually very simple and it does its job very well when they are configured properly but sometimes it might not be working at all.

Now a common issue with WordPress is you have some code like this (htaccess rules are from the W3 Total Cache Plugin):

On most standard issues this should work with 0 problems but there is a prerequisite that you might not be aware of. The first line states the following: <IfModule mod_expires.c>

<IfModule mod_expires.c>

This means that if the apache mod mod_expires is installed then follow these rules. If its not installed then these mods are simply not going to do anything and your files won’t be saved locally.

How To Solve

To solve this issue you can do one of two things. You can contact your host to install mod_expires and they should promptly doing so. However, if you are using a dedicated server and have to do this on your own you might find yourself in a little more difficult situation.

If you are using an Ubuntu server you can run the following command which should install mod_expires.

This first line will install mod_expires onto the server.

sudo a2enmod expires

This line will restart it so mod_expires takes effect. If you are using this on a live site then make sure to do it during a low traffic period and the downtime should be minimal.

sudo service apache2 restart

It Works But Some Files Still Aren’t Being Cached

So in this event, you have run GTMetrix and you have found that some files are still not being served a long caching header through mod_expires and this is normally something obvious.

If the files are not on your server (google analytics, AdSense, or another external script) you are not going to be able to fix their expires heading. This might seem dumb and it could be hurting your performance but there typically is a good reason for it. These scripts are normally quickly evolving and they are going to be changing frequently and thus caching them longer than the time they have set might cause usability issues such as your analytics not showing the latest feature which is why it’s common to wait 24 hours for some data to being reporting.

I hope this resolves any problems you have been experiencing when attempting to set expires headers in WordPress!