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"

Without Expires Headers

No Expires Headers

With Expires Headers

With Expires Header


September 10th, 2007

Comments Welcome

  • 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

  • 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"
    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/
    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

  • referat

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

  • Slavi


    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


    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!

  • Duke

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

  • 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????

  • 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.


    Jeeshen Lee.

  • miranda

    i have lot of images on my wp blog
    with 85 score. on firebug

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

  • 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.

  • 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
  • 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...

    • 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");



    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

  • @askapache · Jul 21
    Magic spells for sending thoughts across time? Books
  • @askapache · Jul 20
    TV is just a relic of the previous generation. We just don't know it yet.
  • @askapache · Jul 20
    I will never go back on my ideals, no matter the cost. I'll never let the economic vultures steal my dreams. I'd rather give up the ghost
  • @askapache · Jul 18
    I don't want a better seat, I want control of the engine
  • @askapache · Jul 14
    No matter how good u r, there will always be someone 2x, 5x, 100x better. This is true for me and everyone. No direction but forward.
  • @askapache · Jul 12
    Heads up, I'll DDoS the f out of askapache next week, to see how resilient it really is :) - will try to overflow disk, net, and ip stack
  • RUN GCC! This is a typical shirt I wear, from the  shop. A clerk at the LQ recognized it! 
  • Merlin the Magician 
  • ROGUE CODE - Latest novel from @markrussinovich 
  • RTFM - surprisingly very helpful and way more comprehensive than it looks! @redteamfieldman #pwnAllTheThings 
  • Dear Hacker - Letters to the Editor of 2600, from Emmanuel Goldstein 
  • The Mythical Man-Month - Essays on Software Engineering, by Frederick P. Brooks, Jr. 
  • "where wizards stay up late" - The Origins of the Internet. Favorite book detailing the birth of the net and IMPs 
  • ZERO DAY - read before Trojan horse 
  • Trojan Horse, a novel! 
  • The Hacker Playbook - very nice high level overview of attacks 
  • Clean Code - A Handbook of Agile Software Craftsmanship 
  • Secrets of the JavaScript Ninja - By my absolute favorite JS hacker John Resig! 
  • Hacking Exposed 7: Network Security Secrets & SolutionsMy all time favorite, basic but thorough and accurate. 
  • Empty words will be no surrogate for cold resolve. Pain is nothing. 

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


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