Code Snippet
Set Expires
Setting "expires" tells browsers downloading these files that they don't need to request it again for this specific length of time. In otherwords, use the cache instead if you have it. This can reduce stress on the server for you, and speed up page load time for visitors.
# BEGIN EXPIRES
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 10 days"
ExpiresByType text/css "access plus 1 week"
ExpiresByType text/plain "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType application/x-javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 week"
ExpiresByType application/x-icon "access plus 1 year"
</IfModule>
# END EXPIRES
I would add that if you are using something like that, embed a datestamp or version identifier in the filenames of resources. That way, if you update a resource you can increment the indicator so that visitors will load the new one instead of continuing to use the old one until the expires period runs out.
Yes! I’ve found this to be critical with flash files.
Can you offer a snippet that would do just that? Sounds like a great idea… but I wouldn’t know how to do it.
This increment thing the two above you are talking about is likely simpler then you think.
It goes like this:
[link rel="stylesheet" href="http://path/style.css?v=001" type="text/css" /]
those of you knowing PHP will recognize the v-parameter and “001″ as a value. Now… what does PHP has to do with CSS exactly ?
Answer: Nothing !
However the browser doesn’t know that and will think that style.css?v=001 is not the same as style.css?v=002 . So, use the snippet above from Chris Coyier and append a v-parameter to the url and whenever the v-parameter changes the cache/setExpires will be ingnored and reset to 0.
Also, if you’re using this in WordPress, I recommend not setting it manually in header.php but make a function in functinons.php like so:
[?php
function version(){
$version = "001";
return $version;
}
?]
# header.php
href="style.css?v=[?php echo version(); ?]"
I’ve always wondered—is there a way to turn this code off in a sense if you decide to make updates to your Javascript or image files for example? I’ve read that you have to rename them, but is there a way around this or do you have to wait for the cached time to run out?
Thanks to the team of CSS-Tricks.com, I appreciate your help, Your website is very resource able. I learned many tricks and tips and still learning.
I have one problem need help please. I am unable to display images from my website in google chrome browser. It works fine in IE and Firefox. When I was researching regarding this issue, I learned that we have to set expire for the images. Can any one please guide me how to set expiry for the images in CSS or else any other way.
Thanks for your all support.
This code is not working on my site . Tell me why?
really nice snippet.
This website is a great resource. no doubt!
Just a quick question: What is the difference between “ExpiresByType application/x-javascript” and “ExpiresByType application/javascript”?
What is “x-javascript” exactly?
thanks
jpg was left out of the filetype list, might save some confussed people some time to add it. Took me a few hours to notice! or is there something i dont know? are jpegs the same as jpgs, well i suppose they are but are they to the server?
jpg was left out of the filetype list, might save some confussed people some time to add it. Took me a few hours to notice! or is there something i dont know? are jpegs the same as jpgs, well i suppose they are but are they to the server i have tha some question
Excellent idea. Thanks for sharing
I have use this code in a couple of website, but it is add, it does not get recognized at all.
This is what I get for example in Yslow:
Grade F on Add Expires headers
http://mywebsite.com/wp-content/themes/theme/scripts.js
What would prevent this code from been added to the expired rules?
Thanks,
Hi , I’ve a file with .css.php extension that didnt take those rules above.
What I should do for this file ????
DigWP
A book and blog co-authored by Jeff Starr and myself about the World's most popular publishing platform.
Quotes on Design
Design, like Art, can be an elusive word to define and an awfully fun thing to have opinions about.
HTML-Ipsum
One-click copy to clipboard access to Lorem Ipsum text that comes wrapped in a variety of HTML.
Bookshelf
Hey Chris, what books do you recommend? These, young fertile mind, these.