Speed Tips: Add Future Expires Headers

FREE THOUGHT · FREE SOFTWARE · FREE WORLD

Speed Tips: Add Future Expires Headers

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.

Easy Apache Speed Tips Articles

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

Add Future Expires Headers

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


«
»

Leave your own comment

Reader Comments

  1. Jack Fisher ~

    When I add

    Header set Expires "Thu, 15 Apr 2011 20:00:00 GMT"
    

    I get the 500 message error

  2. Miki ~

    Hi so if i’m right the .htaccess should look like this:

    # turn on the module for this directory
    ExpiresActive on
    # set default
    ExpiresDefault "access plus 24 hours"
    ExpiresByType image/jpg "access plus 1 months"
    ExpiresByType image/gif "access plus 1 months"
    ExpiresByType image/jpeg "access plus 1 months"
    ExpiresByType image/png "access plus 1 months"
    ExpiresByType text/css "access plus 1 months"
    ExpiresByType text/javascript "access plus 1 months"
    ExpiresByType application/javascript "access plus 1 months"
    ExpiresByType application/x-shockwave-flash "access plus 1 months"
    
  3. Billy ~

    I’m using Yslow, and having a hard time getting any different results using either the:

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

    command, or the ones that phpGangsta posted above. I’m still getting the same results in Yslow. I’m pasting them into my htacess file. I did successfully remove etags earlier, so I know its working, but is there something else I need to know about adding these expiring headers?

  4. Daniel Norton ~

    I presume that this article specifies an expires date in the past because when the article was written, it was in the future.

    Stop pussyfooting, and set the date 15 years in the future, to 2025.

    For those of you getting “Error 500” I’m surmising that you’re using the .htaccess method, as the apache.conf method would have failed when you restarted the server and it would tell you that it didn’t recognize the “header” directive. If you have access to the error log, you’ll see that message in the log for the .htaccess usage.

    The solution? Install the mod_headers module on your Apache server.

  5. Visit my site and it tries to download a GZ file – Page 2 – WordPress Tavern Forum ~

    [...] with expires in .htaccess I used this site as a staring point: http://www.askapache.com/htaccess/ap…d-expires.html Personal Blog | [...]

  6. PHPGangsta ~

    I prefer relative dates, like this:

        # turn on the module for this directory
        ExpiresActive on
        # set default
        ExpiresDefault "access plus 24 hours"
    
    cache common graphics
        ExpiresByType image/jpg "access plus 1 months"
        ExpiresByType image/gif "access plus 1 months"
        ExpiresByType image/jpeg "access plus 1 months"
        ExpiresByType image/png "access plus 1 months"
    
    cache CSS
        ExpiresByType text/css "access plus 1 months"
    cache other filetypes
        ExpiresByType text/javascript "access plus 1 months"
        ExpiresByType application/javascript "access plus 1 months"
        ExpiresByType application/x-shockwave-flash "access plus 1 months"
    
  7. Speed Tips: Turn Off ETags ~

    [...] « Speed Tips: Add Future Expires HeadersSpeed Tips: Remove Last-Modified Header » [...]

  8. Ela ~

    Please can you explain me how to setup this expire header to the httpd.conf file. I have access to httpd.conf through ssh.

  9. Aaron Braun-Duin ~

    I have implemented this code in the htaccess of my WordPress blog, however, I am still getting an F from YSlow for not having expires headers on the adsense code on my site. Is there any way to remedy that, or is that not really having any significant effect on my loading speed despite the poor score? Thanks.

  10. Michael ~

    Thanks for the great content. I came across your site as I was trying to fix a caching problem on one of my websites. I’m a bit of a novice when it comes to caching and did a lot of damage to my site this past week experimenting with far future expires header.

    In your post, you say

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

    I would be very grateful if you could point me in the right direction as to what code I should include in my .htaccess file to fix this problem.

    After I made this initial change, I lost my enter header section of my website and it also screwed up the rest of my page template.

    Thanks in advance for your help!

  11. julien ~

    Hi,

    i just would like to know one thing. if you combine using multiple subdomain to serve images and script, where would put your .htaccess file that hold the cache control header, future expire header and all this for images.

    do you put it in your main domain root or in the subdomains root where you have images and script?

    thanks

    julien

  12. Jeff ~

    Hello, when l add this to my .htaccess it crashes my site (returns 404) … any idea why?

    Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
    
  13. Pete ~

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

  14. 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/

Go for it!


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

HTML | DCMI | GRDDL | XOXO | XDMP | XFN | DOM | XML | XHTML 1.1 Strict | CSS 2.1 | W3C

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.

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

↑ TOP
Main