問題描述
Apache:如何在未安裝 mod_expires 的情況下將 Expires 標頭添加到文件中 (Apache: How to add Expires headers to files without mod_expires installed)
我在運行 Apache 的共享主機上。顯然 mod_expires 沒有安裝。是否有另一種方法可以在該環境中將過期標頭添加到 css 文件、圖像等?
參考解法
方法 1:
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
方法 2:
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)
方法 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.
(by Hedge、ajreal、jensgram、KingCrunch)