speed up your site – set expiration in cache control

Another useful Apache utility called mod_headers can be used to set an expiration date in the future of files like images and static html pages. This means that rather than loading image files or unchanging html code every time you visit a site, a check will be made in the cache of your browser to see if there is already an existing copy of the file requested. If the file is still ‘fresh’ – ie within the period specified by the expiration, the cached version of the file will be used and no ‘refresh’ call will be made to the host server.

Your server needs to have mod_headers enabled before being able to use these directives. You can add this code to your root htaccess file, or http.conf / vhosts.conf if you have access to those.


#cache html and htm files for one day   
   
Header set Cache-Control "max-age=43200"  
   
  
#cache css, javascript and text files for one week   
   
Header set Cache-Control "max-age=604800"  
   
  
#cache flash and images for one month   
   
Header set Cache-Control "max-age=2592000"  
   
  
#disable cache for script files   
   
Header unset Cache-Control   

When changing conf files on an Apache server, remember to Apply Changes and restart the Apache server for the directives to take effect.

Another approach is to use ExpiresByType commands in the htaccess file:

# 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 image/x-icon "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"

Final tip: set eTags to null using:

Header unset ETag
FileETag None