last modified header
Syntax: | add_header name value [always]; |
---|---|
Default: | — |
Context: | http , server , location , if in location |
Adds the specified field to a response header provided that the response code equals 200, 201 (1.3.10), 204, 206, 301, 302, 303, 304, 307 (1.1.16, 1.0.13), or 308 (1.13.0). Parameter value can contain variables.
There could be several add_header
directives. These directives are inherited from the previous configuration level if and only if there are no add_header
directives defined on the current level.
If the always
parameter is specified (1.7.5), the header field will be added regardless of the response code.
add header Last-Modified
Nginx adds last modified date for files cached html files in WordPress cached pages automatically rather than published date, it adds cached or stored date in SSD or disk.
for wordpress we need to add this plugin as of now working .. Last-Modified and If-Modified-Since Headers
Nginx Module ngx_http_headers_module
if_modifeid_since exact;
exact: Returns 304 Not Modified if the date and time specified in the HTTP header are an exact match with the actual requested file modification date. If the file modification date is earlier or later, the file is served normally (200 OK response).
Disable Last modified header in nginx
by default nginx adds last modified header for static files css,js images and html which are stored in disk. not from mysql for this we need to tweak php.
if_modified_since off;
Removing Last-Modified header:
add_header Last-Modified “”;
Cloudflare Nginx Last modified header
When both Last-Modified and Etag headers are absent from the origin server response, Smart Edge Revalidation will use the time the object was cached on Cloudflare’s edge as the Last-Modified header value.
Problem: cache ttl sets for 2 hours the page last modified time always within 2 hours, google may ignore the last modified header or updated genuine pages.
Change alter the last modified date
add_header Last-Modified $date_gmt;
if_modified_since off;
etag off;
As for the last line, if you really want to hide a true last-modified date, then you must hide the ETag
header too since it leaks timestamps.
add_header Last-Modified $date_gmt; (the present date prints)
Allows preserving the “Last-Modified” header field from the original response during SSI processing to facilitate response caching.
Add headers Last modified exact date with PHP
<?php
//get the last-modified-date of this very file
$lastModified=filemtime(__FILE__);
//get a unique hash of this file (etag)
$etagFile = md5_file(__FILE__);
//get the HTTP_IF_MODIFIED_SINCE header if set
$ifModifiedSince=(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false);
//get the HTTP_IF_NONE_MATCH header if set (etag: unique file hash)
$etagHeader=(isset($_SERVER['HTTP_IF_NONE_MATCH']) ? trim($_SERVER['HTTP_IF_NONE_MATCH']) : false);
//set last-modified header
header(“Last-Modified: “.gmdate(“D, d M Y H:i:s”, $lastModified).” GMT”);
//set etag-header
//header(“Etag: $etagFile”);
header(“ETag: \”$etagFile\””);
//make sure caching is turned on
header(‘Cache-Control: private, must-revalidate, proxy-revalidate, max-age=3600’);
//check if page has changed. If not, send 304 and exit
if (@strtotime($_SERVER[‘HTTP_IF_MODIFIED_SINCE’])==$lastModified || $etagHeader == $etagFile)
{
header(“HTTP/1.1 304 Not Modified”);
header(“Vary: Accept-Encoding”);
exit;
}
?>
Last modified header plugins for WordPress
LH Add Headers
Check HTTP Headers in linux terminal
curl -I https://sarkariresultz.in/iti-jobs/
root@ubuntu-s-2vcpu-4gb-intel-blr1-01:~# curl -I https://sarkariresultz.in/iti-jobs/
HTTP/2 200
date: Sun, 19 Mar 2023 10:04:52 GMT
content-type: text/html
last-modified: Sat, 18 Mar 2023 06:26:48 GMT (ETAG on)
vary: Accept-Encoding, Cookie
cache-control: no-cache, no-store, must-revalidate
x-rocket-nginx-serving-static: HIT
cf-cache-status: DYNAMIC
strict-transport-security: max-age=31536000; includeSubDomains; preload
x-content-type-options: nosniff
server: cloudflare
cf-ray: 7aa4e4c7a9949bd7-FRA
alt-svc: h3=”:443″; ma=86400, h3-29=”:443″; ma=86400
for wordpress add this to theme function code
This worked for me on all posts (Not pages)– added into theme functions.php file:
wordpress variables
add_action(‘wp’, ‘last_if_modified_headers’ );
function last_if_modified_headers() {
global $post;
if(isset($post) && is_single()){
$LastModified_unix = strtotime($post->post_modified);
$LastModified = gmdate(“D, d M Y H:i:s \G\M\T”, $LastModified_unix);
$IfModifiedSince = false;
if (isset($_ENV[‘HTTP_IF_MODIFIED_SINCE’])) {
$IfModifiedSince = strtotime(substr($_ENV[‘HTTP_IF_MODIFIED_SINCE’], 5));
}
if (isset($_SERVER[‘HTTP_IF_MODIFIED_SINCE’])) {
$IfModifiedSince = strtotime(substr($_SERVER[‘HTTP_IF_MODIFIED_SINCE’], 5));
}
if ($IfModifiedSince && $IfModifiedSince >= $LastModified_unix) {
header($_SERVER[‘SERVER_PROTOCOL’] . ‘ 304 Not Modified’);
exit;
}
header(‘Last-Modified: ‘. $LastModified);
}
}
use this plugin (how to create a WordPress plugin)
last-modified-and-if-modified-since-headers
copy the following text to the header.php file:
header(“Last-Modified: ” . date(‘r’,strtotime($post->post_modified)));
header("Last-Modified: " . date('r',strtotime($post->post_modified)));
However, this code applies for posts and pages only. It’s useless for the main page, archives, recent comments, and taxonomy.
<?php $LastModified_unix = 1294844676; $Last Modified = gmdate(«D, d M Y H:i:s \G\M\T», $LastModified_unix); $IfModifiedSince = false; if (isset($_ENV[‘HTTP_IF_MODIFIED_SINCE’])) $IfModifiedSince = strtotime(substr($_ENV[‘HTTP_IF_MODIFIED_SINCE’], 5)); if (isset($_SERVER[‘HTTP_IF_MODIFIED_SINCE’])) $IfModifiedSince = strtotime(substr($_SERVER[‘HTTP_IF_MODIFIED_SINCE’], 5)); If ($IfModifiedSince && $IfModifiedSince >= &LastModified_unix) { header ($_SERVER[‘SERVER_PROTOCOL’] . ‘ 304 Not Modified’); exit; } header(‘Last-Modified: ‘ . $LastModified); ?>
global $wpdb;
$wp_last_modified_date = $wpdb->get_var(“SELECT GREATEST(post_modified_gmt, post_date_gmt) d FROM $wpdb->posts WHERE post_status = ‘publish’ ORDER BY d DESC LIMIT 1”);
$wp_last_modified_date = max($wp_last_modified_date, get_lastcommentmodified(‘GMT’));
$last_modified = mysql2date(‘D, d M Y H:i:s’, $wp_last_modified_date, 0) . ‘ GMT’;
//set last-modified header
header( “Last-Modified: “.$last_modified);