I have a PHP file that will return the same thing with the same $_GET parameters every time — it’s deterministic.
Unfortunately for efficiency (this file is requested very often), Apache defaults to a “200 OK” response whenever a PHP page is requested, making the user download the file again.
Is there any way to send a 304 Not Modified header if and only if the parameters are the same?
Bonus:
Can I set an expiry time on it, so that if the cached page is more than, say, three days old, it sends a “200 OK” response?
Without caching the page yourself (or at least its Etag) you cannot really make use of the 304. A full fledged caching algorithm is somewhat out of scope, but the general idea:
All common pitfalls apply: you can possibly not display cache pages for logged in users, a caching of partial content could be more desirable, you are yourself responsible for preventing stale content in the cache (possibly using triggers in backend or database on modifications, or just playing around with the
getUrlEtaglogic), etc. etc.You could also play around with
HTTP_IF_MODIFIED_SINCEif that’s easier to control.