Skip to content

Instantly share code, notes, and snippets.

@ivansky
Last active February 12, 2019 10:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivansky/c020f7d9dfd39fb6aac1 to your computer and use it in GitHub Desktop.
Save ivansky/c020f7d9dfd39fb6aac1 to your computer and use it in GitHub Desktop.
PHP 304 Last Modified Example
# Bitrix Example
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !/bitrix/urlrewrite.php$
RewriteRule ^(.*)$ /bitrix/urlrewrite.php [L]
#RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization}]
RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization},E=HTTP_IF_MODIFIED_SINCE:%{HTTP:If-Modified-Since}]
<?php
$LastModifiedTime = 0; // < == SOME TIMESTAMP WHEN MODIFIED
$LastModified = date('D, d M Y H:i:s') . ' GMT';
$IfModifiedSince = isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : 0;
$IfModifiedSinceTime = strtotime($IfModifiedSince);
if ($IfModifiedSince && preg_match('/[0-9]{2,}\s+[a-z]{2,}\s+[0-9]{4}\s[0-9]{2}:[0-9]{2}:[0-9]{2}/i', $IfModifiedSince) && $LastModifiedTime > $IfModifiedSinceTime){
$sapi_name = php_sapi_name();
if (strrpos($sapi_name, 'cgi') === 0){
header('Status: 304 Not Modified');
}else{
header('HTTP/1.1 304 Not Modified');
}
exit;
}
header('Last-Modified: ' . $LastModified);
/* ... contents of file ... */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment