Rus
Eng
Битрикс: устанавливаем If-Modified-Since заголовки
Order site
By contacting me you will receive a reliable and knowledgeable contractor who will quickly and efficiently implement any task for you and your business.

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

There are no comments yet, you can be the first to leave it

Leave a comment

The site uses a comment pre-moderation system, so your message will be published only after approval by the moderator

You are replying to a user's comment

Send

FEEDBACK

Email me

Please message me to discuss the task. I work remotely and officially (as a self-employed professional); my hourly rate is 1,800 RUB, and I can dedicate 3–4 hours a day to the project. I respond quickly, and the consultation is free.

An error occurred while sending, please try again after a while
Message sent successfully

Phones

+7(993) 007-18-96

Email

info@tichiy.ru

Address

Россия, г. Москва

By submitting the form, you automatically confirm that you have read and accept the Privacy Policy site