问题
I'm on a shared host running Apache. Apparently mod_expires is not installed. Is there another way to add expires headers to css-files, images etc. on that environment?
回答1:
In your .htaccess
(if that's an option) you can use a <FilesMatch> block with Header directives. This requires mod_headers
, though, and I'm pretty sure that you can't specify a "rolling" expiration date (i.e., "one year from now"). Therefore, you'll need to edit this setting, say, once a year1.
Also, did you see this question?
1) Apparently you should refrain from setting the Expires
to more than a year into the future: "Do not set it [the Expires
header] to more than one year in the future, as that violates the RFC guidelines." (source: Optimize caching)
回答2:
You can use PHP to embed expiration headers,
is not efficient as apache mod_expires,
but as least it still able served for primed cache
Using a far future Expires header affects page views only after a user has already visited your site. It has no effect on the number of HTTP requests when a user visits your site for the first time and the browser's cache is empty. Therefore the impact of this performance improvement depends on how often users hit your pages with a primed cache. (A "primed cache" already contains all of the components in the page.) We measured this at Yahoo! and found the number of page views with a primed cache is 75-85%. By using a far future Expires header, you increase the number of components that are cached by the browser and re-used on subsequent page views without sending a single byte over the user's Internet connection.
source: http://developer.yahoo.com/performance/rules.html#expires
回答3:
Just send the Header yourself using header()
header("Expires: Thu, 01 Dec 1994 16:00:00 GMT", true);
Edit: Didnt see, that images, ... are also mentioned. This only works for php files or anything you pass-through php, which in most cases is not a really good idea.
来源:https://stackoverflow.com/questions/4267554/apache-how-to-add-expires-headers-to-files-without-mod-expires-installed