Apache:如何在未安裝 mod_expires 的情況下將 Expires 標頭添加到文件中 (Apache: How to add Expires headers to files without mod_expires installed)


問題描述

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 HedgeajrealjensgramKingCrunch)

參考文件

  1. Apache: How to add Expires headers to files without mod_expires installed (CC BY‑SA 3.0/4.0)

#mod-expires #apache #PHP






相關問題

lighttpd 中靜態圖片的 expires.url 配置 (expires.url configuration for static images in lighttpd)

mod_expires.c 和 mod_headers.c 根據 google pagespeed 見解無法正常工作 (mod_expires.c and mod_headers.c not working according to google pagespeed insights)

Apache FilesMatch - 匹配正則表達式中的文件夾 (Apache FilesMatch - matching a folder in the regular expression)

mod_expires 為 3## 狀態碼發送 Cache-Control 標頭 (mod_expires sending Cache-Control headers for 3## status codes)

Apache:如何在未安裝 mod_expires 的情況下將 Expires 標頭添加到文件中 (Apache: How to add Expires headers to files without mod_expires installed)

PHP session_start() 覆蓋 HTTP Expires 標頭 (PHP session_start() overwrites HTTP Expires header)

添加過期標頭以提高使用 .htaccess 的頁面的性能 (Add expire header to improve the performance of page using .htaccess)

使用 apache mod_expires 緩存 php,php 文件的 mime 類型是什麼? (Use apache mod_expires to cache php, what is a mime type for php files?)







留言討論