Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

My goal
I would like to let browsers cache my whole website, but only download the new information when I have changed one or more files.

My situation
After some research I have found a way to do this. That is to add a Far Future Expires Header to my htaccess file and add a querystring to my files using the filemtime() function.

The problem
When I click on the address bar and type in my website address in firefox, then Firebug displays
38.3 KB (36.4 KB from cache)

When I press F5 in firefox, then Firebug displays:
241.1 KB (10.9 KB from cache)

Now I have tried to do the same with Google and they are sending HTTP header 304 back. I have read a lot about ETag and the Last Modified header, but I have heard a lot of people saying that they are not really reliable.

My question
What would be the best solution if I would like to send HTTP header 304 back if the user presses on F5, like Google?


Update
It seems that Firefox is controlling the way the cache is used and I would like to use the cache also when a user presses F5.

share|improve this question
    
I have updated my question. –  Z0q Mar 3 '11 at 13:44
add comment

3 Answers 3

up vote 0 down vote accepted

As suggested @symcbean, and is the recommended practice to use unique URLs to avoid any problem with stale/new content. Anyway to eliminate the 304 requests remove the ETags and Last-Modified tag.

The URL below provides good tips for speeding/caching web pages.
http://www.askapache.com/htaccess/apache-speed-last-modified.html

share|improve this answer
add comment

When you use Apache2 there is a module called "mod-expires" we use it here for caching. The module set the expire header for the requests.

share|improve this answer
    
Did you read my question? –  Z0q Mar 3 '11 at 13:00
    
Yes i have read it ;) and you want an answer. We use it here. Expire header and Etags. And when you use expire headers you get 304 not modified. @Z0q –  Stony Mar 3 '11 at 13:08
    
When you use expire headers, the browser won't even send requests to the server, so they won't return a 304 header. That will only be sent when you are using ETags or the Last-Modified header. –  Z0q Mar 3 '11 at 13:59
add comment

Avoid conditional requests - unless you are serving up large volumes of very large files (such as videos) then they can actually make your site much slower.

that they are not really reliable.

IME, they are very reliable - but also very inefficient.

but only download the new information when I have changed one or more files.

Don't change files - create new URLs for the new content. You can't rescind caching information already issued.

That is to add a Far Future Expires Header to my htaccess file and add a querystring to my files using the filemtime() function

Oh - sorry, you're already doing that.

Pressing f5 in your browser is not a realistic way to measure cacheability.

Yes you can make a 304 response if the browser makes a conditional request and the conditions are met by the current content - but as above it's not a good way of dealing with the situation - consider you have a spot graphic appearing 4 times on a page. After the cache has expired, the browser will send 4 conditional requests to the origin each time this page is loaded. While you can send new caching instructions in a 304 response, it's usually not very easy to do this in practice. Remember latency is far more of a problem than bandwidth.

share|improve this answer
    
Thank you for your very clear answer. So you would suggest to keep the Far Future Expires Header and the querystring with filemtime(), but to unset the Last-Modified header? –  Z0q Mar 3 '11 at 14:26
    
Yes - and (assuming this apache 2.0 or later) use mod_headers to remove if-modified-since and if-none-match from request –  symcbean Mar 7 '11 at 9:15
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.