No-cache Files with WordPress Multisite

Creating a WordPress Multisite for a client I came across an issue when trying to prevent flash files (.swfs) from being cached (stored by the browser).


WordPress Logo

Creating a WordPress Multisite for a client I came across an issue when trying to prevent flash files (.swfs) from being cached (stored by the browser).

You can’t use normal Apache .htaccess files when serving files from within WordPress multisite because they get funneled through a PHP file, located in wp-includes/ms-files.php. You can check for any filetype, I checked for .swf files. Here’s my modified code:

$last_modified = gmdate( 'D, d M Y H:i:s', filemtime( $file ) );
$etag = '"' . md5( $last_modified ) . '"';
header( "Last-Modified: $last_modified GMT" );
header( 'ETag: ' . $etag ); /* no cache swf files - K.Mo_*/
if(preg_match("/._.swf/i", $file)) {
header( 'Expires: Wed, 11 Jan 1984 05:00:00 GMT');
header( 'Cache-Control: max-age=0, no-cache, no-store, must-revalidate');
header( 'Pragma: no-cache');
}
else {
header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + 100000000 ) . ' GMT' );
}

You’ll need to check if WordPress gets updated because this file is part of the core WordPress install and will likely be overwritten when there’s an update. I’ll keep this page updated if there’s a better way to accomplish this task.