The last notes
All English-language materials have been translated fully automatically using the Google service
The heading Last-Modified
is a fairly common wish of seo specialists. Unfortunately, Bitrix does not allow setting last-modified headers out of the box. So I applied a trick: I split all pages into 2 boolean types - static and dynamic.
- For static pages I set the title by the time of the last modification of the file in which they are located.
- For dynamic pages in each template, I display the last modification of the page by the time the last element was added or updated.
Providing headings
In init.php
we write
/ * Expose headers if-modified-since * /
require ($ _ SERVER ['DOCUMENT_ROOT']. '/ local / libs / if-modified-since.php');
In if-modified-since.php
/ * Expose headers if-modified-since * /
AddEventHandler ('main', 'OnEpilog', array ('CBDPEpilogHooks', 'CheckIfModifiedSince'));
class CBDPEpilogHooks
{
function CheckIfModifiedSince ()
{
GLOBAL $ lastModified;
if (! $ lastModified) $ lastModified = time () - rand (1000, 100000);
if ($ lastModified)
{
header ("Cache-Control: public");
header ('Last-Modified:' .gmdate ('D, d M Y H: i: s', $ lastModified). 'GMT');
if (isset ($ _ SERVER ['HTTP_IF_MODIFIED_SINCE']) && strtotime ($ _ SERVER ['HTTP_IF_MODIFIED_SINCE'])> = $ lastModified) {
header ('HTTP / 1.1 304 Not Modified'); exit ();
}
}
}
}
If-Modified-Since
for static pages
Write somewhere in the file.
GLOBAL $ lastModified;
if (! $ lastModified) {
$ lastModified = strtotime (date ("D, d M Y H: i: s", filectime ($ _ SERVER ['SCRIPT_FILENAME'])));
} else {
$ lastModified = max ($ lastModified, MakeTimeStamp ($ arResult ['TIMESTAMP_X']));
}
If-Modified-Since
for dynamic list of items
In result_modifier.php
if (! empty ($ arResult ['ITEMS'])) {
$ time = strtotime ("- 5 years", time ());
$ date = date ("Y-m-d", $ time);
$ dat ['DATE'] = $ date;
foreach ($ arResult ['ITEMS'] as $ key => $ value) {
if ($ value ['DATE_CREATE'] && strtotime ($ dat ['DATE']) __ component-> arResult ["LAST_MODIFY"] = $ dat ['DATE'];
$ this -> __ component-> SetResultCacheKeys (array ("LAST_MODIFY"));
}
In component_epilog.php
GLOBAL $ lastModified;
$ lastModified = strtotime ($ arResult ['LAST_MODIFY']);
If-Modified-Since
for complex component detail pages
In component_epilog.php
GLOBAL $ lastModified;
$ m = $ arResult ['TIMESTAMP_X']? $ arResult ['TIMESTAMP_X']: $ arResult ['DATE_CREATE'];
if (! $ m) {
$ lastModified = strtotime (date ("D, d M Y H: i: s", filectime ($ _ SERVER ['SCRIPT_FILENAME'])));
} else {
$ lastModified = MakeTimeStamp ($ m);
}
Do not forget to select "Creation date" and "Modification date" in the fields for displaying in the settings of complex components
After adding the code, you will need to update the component cache or completely reset it
I have not tested the compatibility of this solution with the composite
The best place to check the returned headers is here: Last- Modified.com
The solution was applied on the Business v17.5.4 edition
Comments