Caching WordPress Pages with mod_expires

ApacheServerLogo The final step in my WordPress/Apache optimization was to look at mod_expires. The Apache mode_expires module is used to tell a browser how long it can cache the page. With expiration enabled a browser will refresh a page from it’s local cache rather than the server, at least until the page expires.

On my site the pages a relatively static, they may change when a comment is added but that’s about it. The pictures and graphics will almost never change. So I’ll give regular pages a fairly short cache time (5 minutes) and a much longer time for the graphics (30 days). My style sheets also change infrequently so I’ll make those expire after a day.

Unlike other settings like mod_deflate it’s very likely that I’ll want different settings for different sites. So I’ll be setting this in the site file, rather than server-wide like I did for mod_deflate.

I open the site file to edit: sudo nano /etc/apache2/sites-available/

I added the following lines in the <Directory> section of the configuration to apply the settings to my main directory and all sub-directories. I only cache my regular port 80 files, if I access the admin panel using SSL they won’t be cached.

<IfModule mod_expires.c>

ExpiresActive On

ExpiresDefault "access plus 5 minutes"

ExpiresByType image/gif A2592000

ExpiresByType image/jpg A2592000

ExpiresByType image/png A2592000

ExpiresByType text/css A86400



The ExpiresActive turns on the Expires headers. the next line sets a default expiration time for pages, as the name implies. In this case I use the syntax which is more readable. Any page that doesn’t have a expiration specifically defined will expire five minutes after the browser first loads (accesses) it. The next four lines define the expiration times for four specific file types, 3 graphic file types along with css files. These directives use the syntax where A means “accessed” and is followed by the number of seconds to keep the document in cache.

If the expires module isn’t already active you’ll need to enable it with sudo a2enmod expires. Then reload the Apache configuration to enable the new settings.

I looked at my site access logs when loading, and then reloading a page to see the “304” response codes logged on the reloads. I also used the Firefox add-on Live HTTP Headers to look at what the server was sending down. I could see the proper cache settings so I knew all was well.

There’s no guarantee the browser will use the cache settings, but if it does then it could save some bandwidth, time and server resources by loading the file or graphic from the local cache on page reloads.

1 thought on “Caching WordPress Pages with mod_expires”

Comments are closed.

%d bloggers like this: