Speed Up Your Site with htaccess Caching

Advertisement

As far as SEO concern, Google and other search engine like yahoo, bing and msn like fast loading website and fast website is one of the SEO benefits, in this tutorial I will show you two methods to speed up your site both methods are extremely simple to set up and will dramatically speed up your site.

Note: Please do a backup of your .htacess file first before trying these solutions as you need to edit .htaccess file and any miss configuration will laid your site down.


<ifmodule mod_expires.c>
# 1 YEAR
<filesmatch "\.(ico|pdf|flv)$">
Header set Cache-Control "max-age=29030400, public"
</filesmatch>

# 1 WEEK
<filesmatch "\.(jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=604800, public"
</filesmatch>

# 2 DAYS
<filesmatch "\.(xml|txt|css|js)$">
Header set Cache-Control "max-age=172800, proxy-revalidate"
</filesmatch>

# 1 MIN
<filesmatch "\.(html|htm|php)$">
Header set Cache-Control "max-age=60, private, proxy-revalidate"
</filesmatch>
</ifmodule>

<ifmodule mod_headers.c>
# Turn on Expires and set default expires to 3 days
ExpiresActive On
ExpiresDefault A259200

# Set up caching on media files for 1 month
<filesmatch "\.(ico|gif|jpg|jpeg|png|flv|pdf|swf|mov|mp3|wmv|ppt)$">
  ExpiresDefault A2419200
  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 "private, must-revalidate"
</filesmatch>

# Force no caching for dynamic files
<filesmatch "\.(php|cgi|pl|htm)$">
  ExpiresDefault A0
  Header set Cache-Control "no-store, no-cache, must-revalidate, max-age=0"
  Header set Pragma "no-cache"
</filesmatch>
</ifmodule>

If they aren’t available, and you have administrative access, you can recompile Apache to include them.
This can be done either by uncommenting the appropriate lines in the Configuration file, or using the -enable-module=expires and -enable-module=headers arguments to configure (1.3 or greater).

Hope you like this tutorial.

References and Credits

Ask Apache

Advertisement