Technology TidBits

Answers to various technical questions on php programming, mysql, linux, and many more categories.
25% off Hosting at HostGator.com:
Use Code techbits25



How can I set my images and css to be cached by browsers for better performance?

Last updated: 05/21/2012

In apache, using an .htaccess file, you can do this:

 

# 365 days
<filesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=31536000, public"
</filesMatch>

# 3 days
<filesMatch "\.(css|js)$">
Header set Cache-Control "max-age=259200, public"
</filesMatch>

This will set the expiration on your images, flash and video files to 365 days, and your css and js files to 3 days.  (max-age is in seconds)

3 days is a little short, but if you make changes to these files somewhat regularly, just bear in mind that you may have some time before all users see the changes. 

 

blog comments powered by Disqus
Have your own Tech-bit to contribute? Submit it here

Other questions in this category:
How can I rewrite / redirect URLs using apache and mod_rewrite?
How do I password protect a directory on my website?
How do I turn off my website for maintenance?
I'm seeing funny characters or boxes on my website after upgrading / moving to a new server. How do I fix it?
How can I block links from another website to my website?
How can I redirect incorrect hostnames to the proper one?
How can I set up a proper 301 redirect with an htaccess file?
How can I force my php scripts to run php5 ?
How can I see what virtual hosts apache is loading when it starts up
How can I check if a php script is running on SSL?
Why does my site load the Apache manual at the /manual/ URL?
How can I do a simple rewrite for vanity URL's?
What might cause random 404 errors on my website?
How can I redirect 301 old URL's to new ones?



Powered by KnowledgebasePublisher 1.1
Host Gator
Content provided by Roberts WebForge, Inc.