12
Mar
2009

How to boost your website performance by using .htaccess to cache it

Web Talk, WebmasterComments Off on How to boost your website performance by using .htaccess to cache it





Bookmark and Share

As a webmaster, one of the biggest worries of mine as far as my blog is concerned  is, without any doubt, its performance, namely the ability to be downloaded in any browser in almost no time (yes, I am a bit ambitious!). As a matter of fact, the more your website is accessible the more people will be able to see it, above all those ones who  have a slow Internet connection such as dial-up or ISDN connection (and believe me, there are a lot of them, more than you think!). There are a lot of techniques and open source, freeware software that can help you to achieve this result, just browse my Webmaster section to find out more. Anyway, what I didn’t know till yesterday, was that I could get amazing results just by using some nice piece of code to copy-and-paste directly in the .htaccess file located under the public_html folder.

The techniques I am going to write about involve the caching of your blog. But what does it mean?  Basically, when a visitor reads a page in your blog, his browser caches its content in your server. Every time the visitor uses the cache, his browser will send  a “301” which is nothing else than a redirecting instruction to verify if that page has been changed. As you can well imagine, this is quite time and resource consuming, both for your readers and your server. That’s why we can instruct the browser to cache the page for a longer period of time, above all for those files  such as: images, MP3, javascript etc. which will  to stay the same for a certain period of time.

The code below needs  mod_headers enabled in your server. If you don’t know if it is enabled or not, just ask your server provider. Here is the code to copy-and-paste in your .htaccess file.

# 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>

This second code uses mod_expires + mod_headers to work, which have to be enabled in your server.

# Turn on Expires and set default to 0
ExpiresActive On
ExpiresDefault A0

# Set up caching on media files for 1 year (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>

Try both code and find out which one works better in your server!

Source: www.askapache.com




Related Articles Latest Articles
.

Comments are closed.


Copyright © 2007-2017 | Sitemap | Privacy | Back To Top
Best screen resolution 1280x800 or higher.
Web Talk is best viewed in Firefox.

Stat