Browser Caching


#1

I have a question regarding browser caching…

Basically, where can I configure caching for different file types?

Now, for more details… what I’ve noticed is that every time I visit my Dreamhost hosted site, all the images on the page are downloaded. I was viewing the header info, and they don’t seem to be set to cache at all. If I refresh the page (using F5), I get the 304 Not Modified that I would expect to get.

But every time I navigate away and come back, all the images have a 200 Success codes and are being retrieved again.

After researching a bit, I found that some people use .htaccess file to resolve this and set caching, though it appears to be frowned upon. It sounds like a better method it to set caching at the main httpd config file.

So… what I need to know, I guess… is do we have access to that file somewhere (I am using VPS hosting)? If not, is using .htaccess a good method to set caching?

Thanks in advance…


#2

One way is to add this directive to your base level .htaccess file:

<FilesMatch "\.(css|gif|htm|html|ico|jpg|js|png|rdf)$">
Header set Cache-Control "max-age=2592000"
</FilesMatch>

Note: change the file types to accommodate your own specific files.

The above code will tell web browsers to cache those file types for 30 days. This makes for faster loading web pages.

Concerning http config vrs. htaccess. If you are managing your own server or VPS, then use http config (the syntax is a bit different than the above example.)

If your site is on shared hosting, you do not have access to http config so use htaccess (with above code.) Nothing to “frown” about, that’s the way we do it :slight_smile: