这个“304 Not Modified Header”有什么作用呢?就是告诉以前来过浏览器,我这些内容没变过,你不需要重新加载了,省点流量,速度自然也快点。
办法很简单,直接修改wordpress当前模板的“functions.php”文件,输入以下代码就行:
add_filter('wp_headers','wpjam_headers',10,2);
function wpjam_headers($headers,$wp){
if(!is_user_logged_in() && empty($wp->query_vars['feed'])){
$headers['Cache-Control'] = 'max-age:600';
$headers['Expires'] = gmdate('D, d M Y H:i:s', time()+600) . " GMT";
$wpjam_timestamp = get_lastpostmodified('GMT')>get_lastcommentmodified('GMT')?get_lastpostmodified('GMT'):get_lastcommentmodified('GMT');
$wp_last_modified = mysql2date('D, d M Y H:i:s', $wpjam_timestamp, 0).' GMT';
$wp_etag = '"' . md5($wp_last_modified) . '"';
$headers['Last-Modified'] = $wp_last_modified;
$headers['ETag'] = $wp_etag;
// Support for Conditional GET
if (isset($_SERVER['HTTP_IF_NONE_MATCH']))
$client_etag = stripslashes(stripslashes($_SERVER['HTTP_IF_NONE_MATCH']));
else $client_etag = false;
$client_last_modified = empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? '' : trim($_SERVER['HTTP_IF_MODIFIED_SINCE']);
// If string is empty, return 0. If not, attempt to parse into a timestamp
$client_modified_timestamp = $client_last_modified ? strtotime($client_last_modified) : 0;
// Make a timestamp for our most recent modification...
$wp_modified_timestamp = strtotime($wp_last_modified);
$exit_required = false;
if ( ($client_last_modified && $client_etag) ?
(($client_modified_timestamp >= $wp_modified_timestamp) && ($client_etag == $wp_etag)) :
(($client_modified_timestamp >= $wp_modified_timestamp) || ($client_etag == $wp_etag)) ) {
$status = 304;
$exit_required = true;
}
if ( $exit_required ){
if ( ! empty( $status ) ){
status_header( $status );
}
foreach( (array) $headers as $name => $field_value ){
@header("{$name}: {$field_value}");
}
if ( isset( $headers['Last-Modified'] ) && empty( $headers['Last-Modified'] ) && function_exists( 'header_remove' ) ){
@header_remove( 'Last-Modified' );
}
exit();
}
}
return $headers;
}
没有回复内容