How to Add Expires headers

What changes would I need to make to the httpd.conf file to add Expires headers to all html, html, images and css files and javascript files.

The static content (css, images and html files) hardly change so these I would like to set a date of at least 6 months.

However I have one javascript file out of 4 which is likely to change every 3 months. how can I set this so that if the date on the server is changed then the browser always requests the newer one else uses the one in the cache.

The other 3 javascript files I would like to set a date in 1 year.

Thanks in Advance.
singh101Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
David S.Connect With a Mentor Commented:
I was hoping another expert with more experience in this area might reply, but it seems unlikely now.

Anyway, my understanding is that if the file is modified before it's set to expire in the cache, the cached copy will be used unless a user forces the browser to fetch a fresh copy (by clearing their cache or doing a hard refresh).  So if the file may change, don't set the expires header to be too far into the future.


Upon rereading your original question, I see the following:

>  how can I set this so that if the date on the server is changed then the browser always
> requests the newer one else uses the one in the cache.

Perhaps it would be helpful to make sure that your server is configured to use ETags. Basically what they do is allow the browser to ask the server to send a new copy only if the file hasn't been changed.

http://en.wikipedia.org/wiki/HTTP_ETag
0
 
singh101Author Commented:
By the wat we are using Apache 1.3
0
 
David S.Commented:
You can use mod_expires (http://httpd.apache.org/docs/1.3/mod/mod_expires.html) for that.

Try this:
ExpiresByType text/html "modification plus 6 months"
ExpiresByType text/css "modification plus 6 months"
ExpiresByType image/jpeg "modification plus 6 months"
ExpiresByType image/png "modification plus 6 months"
ExpiresByType image/gif "modification plus 6 months"

<FilesMatch "\.js">
ExpiresByType application/x-javascript "modification plus 12 months"
</FilesMatch>
<FilesMatch "special\.js">
ExpiresByType application/x-javascript "modification plus 3 months"
</FilesMatch>

Open in new window

0
 
singh101Author Commented:
Thanks, if for example the date this javascript file was modified on the server is 05/04/2010. I then access the file on the 07/04/2010. I wil get the this file downloaded from the server into my browser cache. Suppose now the file gets modified on the 08/04/2010.

I then access this file - will this give me the new file on the server or will it be served from the cache?
0
All Courses

From novice to tech pro — start learning today.