Whatterz


How to Set an Expires Header in Apache

by Simon. Average Reading Time: about a minute.

Setting an Expires (or Cache-Control) header in Apache will help speed up your website. I’m running Apache 2.x, and define an expires header for all of the site’s static assets (images, stylesheets, and scripts).

In Apache, mod_expires is a module that allows you to set a given period of time to live for web pages and other objects served from web pages. The idea is to inform web browsers how often they should reload objects from the server. This will save you bandwidth and server load, because clients who follow the header will reload objects less frequently.

The expires module is not compiled by default and must be enabled in the Apache httpd.conf file. Make sure the following is present and uncommented (remove preceding the #):

LoadModule expires_module modules/mod_expires.so

To set an expires header, simply add the following to the <virtualHost> section of your Apache vhost configuration:

ExpiresActive On
ExpiresByType image/gif "access plus 1 months"
ExpiresByType image/jpg "access plus 1 months"
ExpiresByType image/jpeg "access plus 1 months"
ExpiresByType image/png "access plus 1 months"
ExpiresByType image/vnd.microsoft.icon "access plus 1 months"
ExpiresByType image/x-icon "access plus 1 months"
ExpiresByType image/ico "access plus 1 months"
ExpiresByType application/javascript "now plus 1 months"
ExpiresByType application/x-javascript "now plus 1 months"
ExpiresByType text/javascript "now plus 1 months"
ExpiresByType text/css "now plus 1 months"
ExpiresDefault "access plus 1 days"

Alternatively you can add it to your htaccess file in an <ifModule mod_expires.c></ifModule> block.

If you need to change the length of time a type expires, simply change access plus 1 months to the appropriate length of time, e.g. access plus 1 days, access plus 365 days etc. The time length is by default specified in seconds, but you may also use any of these keys: years, months, weeks, days, hours, minutes and seconds.

You can read all about expires headers by reading Yahoo!’s Best Practices for Speeding Up Your Web Site guide.

Alternatively, read the Apache mod_expires documentation.

This article has been tagged

, , , , , , , , ,

Other articles I recommend

How to Configure Apache to GZip Your Components

Compressing your Web components will help speed up your Website. The majority of your visitors will benefit as most all Web browsers support GZip compression. You’ll want to compress all text, which includes HTML, CSS, JavaScript, XML, JSON, etc.

Setting up PHP on Mac OSX 10.6 (Snow Leopard)

Since Apple launched Mac OS X 10.5 (Leopard), PHP has been installed by default, albeit disabled. Here is a quick run through of what you need to do to get it up and running.

Configuring ColdFusion 8 with Apache

After installing ColdFusion 8 and Apache successfully you may still see an “HTTP 500 Internal Server Error” when navigating to a ColdFusion page. All is not lost, you simply need to configure, or check the configuration of Apache. Apache requires very little post installation modification, but it is always good practice to check the httpd.conf file to ensure that the ColdFusion “install” scripts did what they were supposed to do.

  • A nice trick is to append a revision number to the static assets. That way when they are changed the latest version still gets served even if the expires header hasn’t … er … expired because the file name will be different.

    Of course this means that the paths to those assets need to be updated too. It seems to me that this would be an ideal thing to do using an Ant script at build time. Maybe using an SVN or similar build number.

  • Leaf.

    Hi Simon.

    Just wondering if you know how to set future expiry for Google Analytics?

  • Welsa Chy

    hi Simon,i followed all your instructions put its not working for me:(…i posted a question on this link:

    http://stackoverflow.com/questions/5401180/error-log-saying-htaccess-expiresactive-not-allowed-here-but-allowoverride-non

    please have a look for more details

  • Welsa Chy

    hi Simon,i followed all your instructions put its not working for me:(…i posted a question on this link:

    http://stackoverflow.com/questions/5401180/error-log-saying-htaccess-expiresactive-not-allowed-here-but-allowoverride-non

    please have a look for more details

  • Welsa Chy

    Hi Simon,
    I followed your tutorial step by step..it keeps getting me an internal error 500.

    i checked my error log..and it kept saying that “ExpiresActive not allowed here” or “ExpiresDefault not allowed here”….This is most strange since i changed “AllowOverride None” to “AllowOverride All” in my httpd.conf file..

    I checked that the mod_expires is being loaded…(i uncommented that line)…

    Can you diagonise the problem here??

  • Welsa Chy

    Hi Simon,
    I followed your tutorial step by step..it keeps getting me an internal error 500.

    i checked my error log..and it kept saying that “ExpiresActive not allowed here” or “ExpiresDefault not allowed here”….This is most strange since i changed “AllowOverride None” to “AllowOverride All” in my httpd.conf file..

    I checked that the mod_expires is being loaded…(i uncommented that line)…

    Can you diagonise the problem here??

  • @welsa
    What version of Apache are you running?
    Does it have mod_expires installed?
    Simply uncommenting the LoadModule line does not necessarily mean mod_expires exists in your Apache installation.
    Have you restarted Apache since changing the configuration?
    Have you copied your expires code directly from this blog post?

  • @welsa
    What version of Apache are you running?
    Does it have mod_expires installed?
    Simply uncommenting the LoadModule line does not necessarily mean mod_expires exists in your Apache installation.
    Have you restarted Apache since changing the configuration?
    Have you copied your expires code directly from this blog post?

  • Pingback: 2011-28-03 web Dev 1: group meetings tomorrow tuesday « Web Development KEA()

  • Welsa Chy

    Hi Simon..i couldn’t access your website for several days in a row..and i gave up..
    It’s now that i see your response..

    I am using XAMPP 1.7.3 with Apache 2.2.14(IPv6 enabled) + openSSL 0.9.8l

    I have restarted my Apache each time i made any change

    Yes i have copied the codes directly.

    I have checked the error.log and the error i constantly get is “AllowOverride not allowed here”

    I am testing it on Localhost.

  • Thank you Simon, very useful article. Maybe, one thing to add is ETAGS, which help web browsers manage cached objects better, by providing a way to identify if a certain object has changed on the server or not. More info can be found in the same Yahoo Guide [ http://developer.yahoo.com/performance/rules.html#etags ] that you mention above. Regards, Veselin Kantsev Linux Laboratories

  • Entity Tags are generally annoying to implement so I generally set FileETag to be none in Apache or htaccess (this requires mod_headers).

    The problem with ETags is that they are constructed to be unique to a specific resource on a specific server. For busy sites with multiple servers, ETags can cause identical resources to not be cached, degrading performance.

    By disabling ETags you disable caching and browsers from being able to validate files and they are forced to rely on the Cache-Control and/or the Expires header.

  • thanks for this, good article. however your article editor replaced some of the ” with “ and ”. If someone (like me) just copies this into the site config, the apache configtest fails:
    service apache2 restartSyntax error on line 10 of /etc/apache2/sites-enabled/ttrss:ExpiresByType takes two arguments, a MIME type followed by an expiry date codeAction ‘configtest’ failed.The Apache error log may have more information. failed!

  • Titus

    Hey, just ran across this article.

    One thing to note: if you are copying the expires section into your apache config be aware of the weird quoting that starts with ‘application/javascript’ and ends on ‘text/css’ , it will cause the apache config to break. Simply restore those to normal quotes to get it working.

    Cheers,

  • Christian Müller

    Thanks Michael, you saved my day!

  • Thanks Michael. I’ve belatedly updated the quotes.