« Firefox, Firebug, and yslow are REQUIREDSpeed Tips: Turn Off ETags »
Speed Tips: Add Future Expires Headers
A 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.
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>
« Firefox, Firebug, and yslow are REQUIRED
Speed Tips: Turn Off ETags »
The love of liberty is the love of others; the love of power is the love of ourselves.
-- William Hazlitt
The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect. Tim Berners-Lee
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"
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?
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.
[...] with expires in .htaccess I used this site as a staring point: http://www.askapache.com/htaccess/ap…d-expires.html Personal Blog | [...]
I prefer relative dates, 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"
[...] « Speed Tips: Add Future Expires HeadersSpeed Tips: Remove Last-Modified Header » [...]
Please can you explain me how to setup this expire header to the httpd.conf file. I have access to httpd.conf through ssh.
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.
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!
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
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"
That middle part actually gives me 500 error. is there another way?
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/
Tags: Apache, askapache, Cache, Cache-Control, compression, CSS, Etags, expires header, FilesMatch, Flash, Htaccess, HTTP Headers, httpd, httpd.conf, Last-Modified, Optimization, PDF, Scripts, SPEED, SSI, YSlow,
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.
When I add
I get the 500 message error