Add Expires headers to your .htaccess file

Expires headers let your browser know whether to serve a cached version of the page and it’s components or to download new versions of the files. By defining when a component of your site expires, you help to:

  • Reduce server load
  • Increase page load time
htaccess Expires headers

What are Expires Headers?

Expires headers tell the browser whether they should request a specific file from the server or whether they should grab it from the browser’s cache.

The whole idea behind Expires Headers is to reduce the number of times a file is downloaded from a server and allow the browser to utilize a previous version of a file that is already saved on your computer. Doing this reduces the number of HTTP requests for the server when someone returns to your website because you are able to reuse the previously download version of the same file.

Web pages have continued to grow in complexity with numerous files download on every page; HTML, CSS, JavaScripts, graphics, images, pdfs and even applications and sometimes up to 25+ items per page. When you visit a website for the first time in your browser, all of these files are requested from the server.  Each request takes time and can add up. By reusing cached versions of these files on repeat visits to the site, you cut down on the number of requests going out to the server and decrease the time at which the web page will be loaded.  Fast load times means happy browsing!

How does Expire Headers work?

Expires Headers tell the web browser how long to store a file in the cache. Keeping the files in the cache of your computer means on future visits to that page the browser will not need to download the file again.

Note: Expires Headers don’t improve page speed for a first visit, after all you need to have downloaded the files at least once to reference them again. But for return visitors, page load times will be faster.

Expires headers can be set on file types or specific files. When the browser comes to the website it can see when was the last time it downloaded the specific file types. If it was recently it will display them from the cache, if you haven’t visited the site in a while it will download the newest version from the web server. If a file is missing from the cache it will be downloaded from the server.

For items that change rarely on your site, like your logo or favicon, you can set the images to expire later. For items that tend to change more often, you can shorten the time. Again you can do this on a per file or file type basis.

How to Add Expires Headers to an .htaccess file

First, you need to decide what files you update often and what file types don’t get updated often. Here is a common list of file types to include:

  • images: jpg, jpeg, gif, png
  • favicon/ico
  • javascript
  • css
  • pdf
  • shockwave-flash

Now go through these file types and think how often you change each one. Commonly images typically are not changed too often (keep in mind we are talking about existing images) favicon is almost never changed, javascript is occasionally changed and CSS is changed most often.

Expires Headers are defined in the .htaccess file for WordPress sites. This is a hidden file often found in the root of your website (via FTP). Before you do anything, backup this file before you change anything. We would hate to see any changing you make knockout your whole website without you being able to revert back to a backup.

Now, open up your .htaccess file in a text editor and paste in the following:

## BEGIN Expires Caching (Leverage Browser Caching) ##
<IfModule mod_expires.c>
# Enable file expirations
ExpiresActive On
# Specific file type expirations
# My favicon
ExpiresByType image/x-icon “access plus 1 year”
# Images
ExpiresByType image/gif “access plus 1 month”
ExpiresByType image/png “access plus 1 month”
ExpiresByType image/jpg “access plus 1 month”
ExpiresByType image/jpeg “access plus 1 month”
ExpiresByType text/css “access plus 2 week”
# Javascript
ExpiresByType text/x-javascript “access 2 week”
ExpiresByType text/javascript “access plus 2 weeks”
ExpiresByType application/x-javascript “access plus 2 weeks”
# HTML and XML
ExpiresByType text/html “access plus 2 weeks”
ExpiresByType application/xhtml+xml “access plus 2 weeks”
# Default file expirations
ExpiresDefault “access plus 2 week”

First, we enable file expirations. Then we added specific lines to make specific files and file types expire. Now go back and change the dates to reflect the times that best suit your website. Lastly, we set a default expiration time for files we don’t specify. Be sure to save your changes and upload the new version of your .htaccess file. Boom, you are done.

Need to add Expires Headers to an asp.NET site? Check out this article.

2017-05-18T13:07:59+00:00 By |