0

形成这个问题(ETag vs Header Expires)我知道我需要 Expire 标头和 eTag 标头,因为它们有两个不同的目的。因为我正在开发一个 Wordpress 插件,所以我不知道如何配置服务器,所以我想知道如何检查 eTags 是否打开。我可以

header( 'Content-Type: text/css' );

// Aggressive caching to save future requests from the same client.
$etag = '"' . md5( __FILE__ . $_GET['my-css-var'] ) . '"';

header( 'ETag: ' . $etag );
header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + 31536000 ) . ' GMT' );
header( 'Cache-Control: public, max-age=31536000' );

if ( empty( $_SERVER['HTTP_IF_NONE_MATCH'] ) || $etag != stripslashes( $_SERVER['HTTP_IF_NONE_MATCH'] ) ) {
// return the CSS
} else {
    // Not modified!
    status_header( 304 );
}

这将在 Etag 关闭和 Etag 开启的情况下工作,或者如果 ETag 关闭会导致一些错误?

4

1 回答 1

1

The web server does not need to have Etag handling turned on for your custom header to work. It will still be sent to the client.

If your server has Etags turned off, the web server will not generate Etags for static files, i.e .jpg, .css, .js.

In Apache you can unset Etags with the following config:

<IfModule mod_headers.c>
  Header unset ETag
</IfModule>

FileETag None

Since this is specific for Apache, and other web servers have different syntax, its a hard thing to check for in your script. But if it is set, it will remove your custom set Etag.

This is not default in Apache though.

To answer your question, no you cant check if Etags is on. But for your purpose I would not worry.

于 2012-07-24T08:01:13.240 回答