Caching web site files that do not change frequently can decrease load time for a web site significantly. Decreased load time means that pages load faster. This could mean the difference between a visitor remaining on your site or leaving, and it could mean the difference between a page one spot in the Google search results and a spot much deeper in the SERPS. Yes, page load time can affect search enginge optimization as well. With that said, the mods we will be doing in this article only effect subsequent visits, not first time visits.

Caching essentially involves placing a file in the “browser cache” (on the visitors hard drive) so that the file loads quicker for subsequent visits. This is meant for files that do not change frequently… static files. Examples include: images, cascading style sheets (CSS files), JavaScript files, etc.

page load time

1 Plugins Exist, Why Not Use Them?

Sure, plugins exist for certain content management systems that implement caching with just a few clicks. W3 Total Cache is an example of such a plugin for use in the WordPress environment, and it is a popular one at that.

But I know that some people are “anti plugin” with the mindset that plugins potentially open backdoors into a site or slow the site down, and may just want to use just one specific feature of the plugin or add on anyway. The rest of the code and feature set may just be overkill in some situations.

2 Modify The .htaccess File To Add HTTP Headers To Cache Static Files

We talked about HTTP headers recently when we talked about the HTTP Header Check Tool on this site. Well, there is a simple way that we can add some lines to an .htaccess file to let us “cache” certain files based on file type.

The way it works is we list out filetypes and decide when they are to expire. What that means is that when a visitor re-vists a page, the browser will load the files from the cache if they have not yet expired, because it’s quicker (saves bandwidth and server resources). After the “expiry date” which may be six weeks past the first time the file was “fetched” for example, the browser will request the file again in the event that it has changed.

Most web servers in the PHP environment use Apache. The following command is for use in that environment. We can use the “ExpiresDefault” directive to set our expiry dates (relative to today’s date) for the static files that we select to be cached.

An example of use of the “ExpiresDefault” directive:

ExpiresDefault "access plus 6 weeks"

This will, as you’ve probably guessed, set the expiry date for the file to 6 weeks from the time of caching. Years, months, days, hours, minutes, and seconds could also be used in place of weeks.

Here is some code that can be placed in an .htaccess file to cache all images, JavaScript files, and CSS files for 6 weeks:

<FilesMatch "\.(ico|jpg|jpeg|png|gif|js|css|swf)$">
ExpiresDefault "access plus 6 weeks"
</FilesMatch>

3 Caching Benefits Occur On Subsequent Visits Remember

Of course, as mentioned these changes do not affect the first time someone visits a site. The “caching” occurs during the first visit. The benefits of the caching happen on each visit thereafter until the expiry date (or when the visitor clears their cache), at which time the process begins again.