How to Set an Expires Header in Apache

Setting an Expires (or Cache-Control) header in Apache will help speed up your Web site.  We’re running Apache 2.x, and define an expires header for all of our static assets (images, stylesheets, and scripts).

Just add the following to your <VirtualHost> section of your Apache configuration:

ExpiresActive On
ExpiresByType image/png “now plus 365 days”
ExpiresByType image/jpeg “now plus 365 days”
ExpiresByType image/gif “now plus 365 days”
ExpiresByType application/javascript “now plus 365 days”
ExpiresByType application/x-javascript “now plus 365 days”
ExpiresByType text/javascript “now plus 365 days”
ExpiresByType text/css “now plus 365 days”

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

Also, be sure to check out our post on how to speed up your Website by Configuring Apache to Gzip Your Components.

Tuesday, December 30th, 2008 Programming

5 Comments to How to Set an Expires Header in Apache

  • Geni says:

    How do I set expiration date using the Header directive in Apache?
    One can do Header set Expires “Thu, 15 Apr 2010 20:00:00 GMT”

    but how do you specify something like access plus 1 day ???

  • sreeyesh says:

    how to set expires header for .swf files

    Thanks,
    Sreeyesh

  • Borders says:

    “…ExpiresActive On
    ExpiresByType image/png “now plus 365 days”
    ExpiresByType image/jpeg “now plus 365 days”
    ExpiresByType image/gif “now plus 365 days”
    ExpiresByType application/javascript “now plus 365 days”
    ExpiresByType application/x-javascript “now plus 365 days”
    ExpiresByType text/javascript “now plus 365 days”
    ExpiresByType text/css “now plus 365 days”…”

    Could this be placed in mod_headers.c?

  • hi excellent post – How to Set an Expires Header in Apache | Justin Britten ,If you want more visitors to your blog you should check this out – http://bit.ly/JspDCe , l8rs Donny Giacomazzi

  • Right here is the perfect website for anybody who wants to understand this topic. You know a whole lot its almost tough to argue with you (not that I really would want to…HaHa). You definitely put a fresh spin on a subject which has been discussed for years. Great stuff, just great!

  • Leave a Reply

    Justin has been obsessing over writing simple Web software using Ruby on Rails since 2007. He's also an entrepreneur and Lean Startup expert. Learn more

    View Justin Britten's profile on LinkedIn

    Subscribe to Justin Britten's blog Follow Justin Britten on Twitter Network with Justin Britten on LinkedIn
     
    Prefinery: Simple, online beta management software'

    Launch a private beta for your Web application in minutes. Prefinery takes care of collecting e-mail addresses, generating invitation codes, and sending invitations for your private beta. Your customers never leave your site, and e-mail invitations are sent from your address.

    Justin is Founder and CEO of Prefinery.