FREE THOUGHT · FREE SOFTWARE · FREE WORLD

Home  »  Htaccess  »  Speed Tips: Add Future Expires Headers

by 21 comments

With Expires HeaderA first-time visitor to your page will make several HTTP requests to download all your sites files, but using the Expires header you make those files cacheable. This avoids unnecessary HTTP requests on subsequent page views. Expires headers should be used on all components including scripts, stylesheets, and Flash components.


Add Future Expires Headers

Keep in mind, if you use a far future Expires header you have to change the component's filename whenever the file changes.

This goes in your root .htaccess file but if you have access to httpd.conf that is better.

This code uses the FilesMatch directive and the Header directive to add Future Expires Headers to certain files.

<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
</FilesMatch>

Without Expires Headers

No Expires Headers

With Expires Headers

With Expires Header

Tags

September 10th, 2007

Comments Welcome

  • http://www.aqworks.com Christophe

    It might be helpful for readers to also mention that you are using Yahoo's Yslow addon for the Firebug extension to get the analysis presented in the screen shots (without/with).

    YSlow for Firebug
    http://developer.yahoo.com/yslow/

  • Pete

    That middle part actually gives me 500 error. is there another way?

  • John

    Hi - having loading problems with my WP sites even though there's very little on them - bearing in mind I'm not a techie, I went to yslow to see if i could understand what was going on - my homepage response time is 13477ms - the details are below:

    Date  Tue, 24 Aug 2010 15:44:50 GMT
    Server  Apache
    P3P  CP="PSA"
    X-Pingback  http://mysite.com/xmlrpc.php
    Expires  Wed, 11 Jan 1984 05:00:00 GMT
    Cache-Control  no-cache, must-revalidate, max-age=0
    Pragma  no-cache
    X-Powered-By  W3 Total Cache/0.9.1.1
    Vary  Accept-Encoding
    Last-Modified  Tue, 24 Aug 2010 15:44:52 GMT
    Connection  close
    Transfer-Encoding  chunked
    Content-Type  text/html; charset=UTF-8

    One thing i notice is that there's an Expires date way in the past of 1984 - I have no idea what an Expires date is or what it means, but that doesn't look right at all - anyway, it might be irrelevant, i just don't know - would appreciate if you could explain what you think is going on here. Thanks

  • http://x-referat.ro referat

    Yeee haaa! I get a 95 points score because of your tip :X thanks alot man! keep up the good posts :X

  • http://devcha.com Slavi

    hm,

    Why adding Expires header to point to the past ?

    This will remove the warning from yslow but will make browsers not to cache files.

    Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
  • Aguitta

    Hello,

    Thanks, great content!. I want what is best for SEO, in terms of expiration date. I think it might be the "Present day".

    So in terms of SEO what is best? and how does that translate to code in the .htaccess?
    Thank you very mucho!

  • http://www.dukesgames.net/ Duke

    Where exactly do i add the code in the htaccess file.

  • http://rooturaj.com Pattanaik

    I get this working for all files withing my site.
    This time I went for some cloud CDN to serve static content. Now the htaccess is not supposed to be working for the cloud files so what to do?
    How do you tell the browser not to fetch content before a particular period ..from the clouds too????

  • http://jeeshenlee.wordpress.com/ JeeShen Lee

    Hello All,

    For ASP user like me, Expire Header can be turn on using web.config file. Refer my blog post below for more information.
    "How to Add Expires Headers in ASP.NET.

    I have tested in on GoDaddy Window Shared hosting and it works.

    Thanks.

    Regards,
    Jeeshen Lee.

  • http://models-hq.com miranda

    i have lot of images on my wp blog http://models-hq.com
    with 85 score. on firebug

    i need to know where to add the code in htaccess badly...

  • http://www.aloe-vera.org David H.

    I can't use it, apparently, on a site created with MS FrontPage (2000). I am using it between the tags, before the body tags.

    My site is on an Apache server.

  • http://www.pc-manden.dk pc-manden

    If your server doesn't support the module mod_expires.c, you'll get a 500 Server Side Error when trying to invoke it with Expires statements.

  • web design houston

    This could also be done with:

    ExpiresActive On
    ExpiresByType image/gif A2592000 # + 1 month after being accesed
  • http://zulutrade-fx.net/ zulu

    Nice. In this way, We don't need mod_expires.c.

  • henk

    ...and yet, nobody comes with an answer what to do when you're provides doesn't allow adding this code in your .htaccess...

    • http://www.askapache.com/ AskApache

      @henk ~

      I strongly suggest you switch to WiredTree.

  • Gaurav

    function clearBrowserCache() {

    header("Pragma: no-cache");

    header("Cache: no-cache");

    header("Cache-Control: no-cache, must-revalidate");

    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

    }

    clearBrowserCache();

    and this code is not working with Add Expires headers

  • Pingback: Does your website load quickly enough? Tips and Tricks to increase site speed

  • Pingback: Complete Guide to Website Speed Optimization | Guest Post Labs

  • Pingback: Prevent caching of XML files with Apache .htaccess rules | Yin Yang Source

  • Dougle

    Don't forget to escape that dot in the FilesMatch regex, you are asking for trouble if you don't.

Popular Articles
My Online Tools

Related Articles
Newest Posts
Twitter


  •  t.co/ShKrGdqXuJ 
  • RUN GCC! This is a typical shirt I wear, from the  t.co/46LYbFr4k2  shop. A clerk at the LQ recognized it!  t.co/jjmT0dkCPu 
  • Merlin the Magician  t.co/iMmRbanUi4 
  • ROGUE CODE - Latest novel from @markrussinovich  t.co/apkn0LoPIt 
  • RTFM - surprisingly very helpful and way more comprehensive than it looks! @redteamfieldman #pwnAllTheThings  t.co/xiaJ5g0aC9 
  • Dear Hacker - Letters to the Editor of 2600, from Emmanuel Goldstein  t.co/JCfLab7FAJ 
  • The Mythical Man-Month - Essays on Software Engineering, by Frederick P. Brooks, Jr.  t.co/ilWN5GHElr 
  • "where wizards stay up late" - The Origins of the Internet. Favorite book detailing the birth of the net and IMPs  t.co/gY9VTGJgZz 
  • ZERO DAY - read before Trojan horse  t.co/pPMLGDJv8P 
  • Trojan Horse, a novel!  t.co/Hf8EtYaZVa 
  • The Hacker Playbook - very nice high level overview of attacks  t.co/lHwNVWi61u 
  • Clean Code - A Handbook of Agile Software Craftsmanship  t.co/hnJX0x1qIc 
  • Secrets of the JavaScript Ninja - By my absolute favorite JS hacker John Resig!  t.co/tZ42ljmcCl 
  • Hacking Exposed 7: Network Security Secrets & SolutionsMy all time favorite, basic but thorough and accurate.  t.co/jycW0RDVtZ 

Hacking and Hackers

The use of "hacker" to mean "security breaker" is a confusion on the part of the mass media. We hackers refuse to recognize that meaning, and continue using the word to mean someone who loves to program, someone who enjoys playful cleverness, or the combination of the two. See my article, On Hacking.
-- Richard M. Stallman






[hide]

It's very simple - you read the protocol and write the code. -Bill Joy

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 License, just credit with a link.
This site is not supported or endorsed by The Apache Software Foundation (ASF). All software and documentation produced by The ASF is licensed. "Apache" is a trademark of The ASF. NCSA HTTPd.
UNIX ® is a registered Trademark of The Open Group. POSIX ® is a registered Trademark of The IEEE.

| Google+ | askapache

Site Map | Contact Webmaster | License and Disclaimer | Terms of Service

↑ TOPMain