Question

Form this question ( ETag vs Header Expires ) i understood that i need both Expire headers and eTag headers because they serve two different purpose. Since i'm working on a Wordpress plugin i don't know how the server will be configured and so i was wondering how to check if eTags are on. I could do

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 );
}

this would work both with Etag off and Etag on or would cause some errors if ETag are off?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top