问题
It seems that during some tests, Firefox will not cache images that are dynamically generated (or loaded from the cache serverside).
I tried enabling caching using headers:
$expires = 60*60*24*14;
header("Pragma: public");
header("Cache-Control: maxage=".$expires);
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');
But to no avail. I still get a "200 OK" instead of "304 Not Modified".
Seeking an answer using the search on Stackoverflow, i discovered a hint by someone that they would rather relocate the browser to the image directly.
Like:
header("Location: /img/generated/whatever.png");
I don't like the approach because it causes two requests.
Could anyone help me out and let me know how i could just get the browser to cache these files?
The Request URL is not changing and it does not contain any seeds or timestamps.
回答1:
You might try the other way around. If you do not want to relocate your browser. You might rewrite your URL using .htacess or/and mod_rewrite or other webserver dependent modules. As example the following URL:
http://sub.domain.com/img/gen/<param>/.../<paramN>/image.png
will be forwarded (without client side redirection) to your script having the param values in the URL. This means you will have only one request.
Examples to do something like this can be found here:
- .htaccess dynamic imageurl rewrite
- mod_rewrite dynamic image names
- http://www.barneyb.com/barneyblog/2009/05/19/efficient-caching-with-mod-rewrite/
回答2:
What you are missing here is last modified header in your reponse header. ex Last-Modified:Wed, 15 Feb 2012 10:07:48 GMT. you can solve this by using this line with your headers.
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
The reason why you should have this because the browser uses last modidfied header to compare if the file is not modified thus no need to re download the file. :
来源:https://stackoverflow.com/questions/8820637/browser-not-caching-generated-images