Recommended:

  • phpclasses.org
  • jsclasses.org
  • jsmag.com
  • siteapps.com
  • View our reviews on Hot Scripts
  • JS Tutorial
  • scripts.com
  • securesignup.com




Recent Comments

Powered by Disqus




Back to articles

Caching using mod_expires and mod_headers

With all growing traffic to websites, heavy graphic files and multiple simultaneous visitors, sometimes you need to use every tool you can, to optimize your site loading and achieve bets performance. One of the thing you must consider then is caching. 

Main advantage of caching is that there is no need to download file again if it was already downloaded once by browser. And disadvantage of course it that, if file was changed on server, changes won't appear to end user, while cache won't expire. So you must consider which files to cache and for how long time period.

Here is an example of how to do caching from Apache side with recommendations of which files to cache to which time periods. Just create .htaccess file in your website root directory and place code below in it. Of course you'll need to edit file types and time to suit your needs:

# Turn on Expires and set default to 0
ExpiresActive On
ExpiresDefault A0
 
# Set up caching on media files for 1 year (consider it forever)
<FilesMatch ".(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$">
ExpiresDefault A29030400
Header append Cache-Control "public"
</FilesMatch>
 
# Set up caching on media files for 1 week
<FilesMatch ".(gif|jpg|jpeg|png|swf)$">
ExpiresDefault A604800
Header append Cache-Control "public"
</FilesMatch>
 
# Set up 2 Hour caching on commonly updated files
<FilesMatch ".(xml|txt|html|js|css)$">
ExpiresDefault A7200
Header append Cache-Control "proxy-revalidate"
</FilesMatch>
 
# Force no caching for dynamic files
<FilesMatch ".(php|cgi|pl|htm)$">
ExpiresActive Off
Header set Cache-Control "private, no-cache, no-store, proxy-revalidate, no-transform"
Header set Pragma "no-cache"
</FilesMatch>

You may also be interested in:

Powered by BlogAlike.com

blog comments powered by Disqus