Windows.  Viruses.  Notebooks.  Internet.  office.  Utilities.  Drivers

The Last-Modified HTTP header tells the client the time last change page (object). If the client (browser, search robot) received the Last-Modified header, then the next time the address is accessed, provided that the page (object) is in the local cache, it will add an If-Modified-Since question (whether the page has changed since the date received in Last-Modified). In turn, the server, having received an If-Modified-Since request, must check the received timestamp with the time the page was last modified and, if the page has not changed, respond with 304 Not Modified.

Save Traffic

If the page has not changed, then the server will stop transmitting data after sending headers with the 304 Not Modified code, the page body, images and other objects will not be transmitted.

Reduced server load

The correct implementation of checking the time of the last modification of the page can significantly (up to 30% or more) reduce the load on the server. Correct implementation means checking the time before the page generation starts on a dynamic site. In this case, all actions for generating the page (requesting content from the database, parsing templates, receiving comments, etc.) will not be performed. This is especially true for sites with high traffic and a long duration of the user's visit. Example: The user is on a sports news site and is constantly updating home page pending publication of the result of the match. In a few minutes, a page can be requested and received dozens of times. If the Last-Modified header is given and the If-Modified-Since request is processed correctly, then the page will actually be submitted once, and all subsequent requests will receive a 304 Not Modified response.

Speed ​​up indexing by search engines

Search engines recommend sending the Last-Modified header and properly handling If-Modified-Since through the Webmaster Guidelines.

The note: the adaptive version of the site is activated, which automatically adjusts to the small size of your browser and hides some details of the site for ease of reading. Enjoy watching!

Hello dear readers of the blog We continue the topic, one of the most important SEO factors. This article will touch on what can be called the intricacies of internal optimization, as it will focus on the response code that search engines and visitors will receive in response to their access to the page.

Correct server response

Despite the fact that this is a rather small detail when building and optimizing the site as a whole, however, it is very important! Namely, it is important that a page on which there have been no changes since the last visit of a robot or a person gives a 304 code, which means that the page has remained unchanged. When the server sends this code to the client, the execution of all PHP scripts on the page does not even start, instead the page is loaded from the cache, which significantly reduces the load on the server and speeds up the page loading for the user.

Thus, by setting up the correct responses of our server, we kill at least five birds with one stone:

  • We speed up page loading for visitors (people).
  • We reduce the load on the server.
  • IN search results will (for Yandex exactly) show the date latest update page, which may grab the user's attention, especially if the date is recent.
  • Site pages will be sorted search engines by date.
  • Significantly accelerate the indexing of the site by search engines!

For some reason, for me, the last point seems to be the sweetest (as it affects SEO and increases the credibility of your site with search engines), although, without a doubt, the other points are also extremely important.

How to configure 304 and 200 server responses?

We have already said that in response to a request to unchanged pages, the server should return 304 Not Modified, and what code should the server return if the client accesses the page for the first time or accesses a page that has changed? In such cases, the server should return the status 200 OK. Specially given code it is not necessary to send, if everything is in order with the page, then it always gives out 200.

Therefore, we only need to take care of the 304 code, since the server will not send it without our intervention. To do this, it will help us, as well as the title Last Modified and request.

Titles Last Modified

Last Modified is the header we send with using PHP, this header contains the exact time the page was last modified (in seconds). To do this, the generally accepted measure of time measurement is used: Unix Time Stamp.

Unix time stamp is the number of seconds since the beginning of the Unix epoch: January 1, 1970. As of this writing, the Unix time stamp is 1370597447 seconds, which is 06/07/2013 09:30:47 GMT (+00:00).

That is, all we need to do is just send a PHP header with the instruction Last Modified and desired date:

Header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_modified_time)." GMT");

Where header is a construct for sending an HTTP header, Last Modified- what we send and immediately after the colon comes its value:

Gmdate("D, d M Y H:i:s", $last_modified_time)." GMT".

The last-modified value is the function gmdate(), which contains the variable I invented $last_modified_time(you can call it whatever). In a variable $last_modified_time and contains the time of the last modification in the format Unix Time Stamp, and the function gmdate() serves us in order to bring the date into the proper form (Greenwich Mean Time).

For clarity, here is an example for you: if we are in a function gmdate() put the value 1365003142 , then the output will be: Wed, 03 Apr 2013 03:32:22 PM.

Now that we have learned how the whole process works, the question may arise: “Do we need to manually indicate the time of the last modification for each page?”. Answer: "Yes!" Personally, I do just that - manually, the most reliable option. However, specifically for this blog, I provided everything, for example, if new comment on the page, then into a variable $last_modified_time the time when this comment was added is recorded, this is done so that search engines can index new comments and know that the site is “live”. Each site is different and you will have to come up with your own algorithm for specifying the date of the last modification of the page, or always specify it manually.

Once again, I emphasize that my algorithm is as follows:

1) I indicate the date of creation of the material manually, if I change something in the article (typos or add), then again I manually enter the new time of the last update.

2) If the visitor adds a comment, then in the variable $last_modified_time automatically, without my knowledge, the time when the comment was added is entered, since in fact this will be the date the page was last modified.

What I did not take into account: in the right column of the site I have fresh articles, recommended And top 10. They change constantly and at the same time for all pages. If every time I change the right column of the site I changed (automatically or manually - it doesn’t matter) the date the page was last modified, then the whole point of this action would be lost. I decided that these changes should be tracked and taken into account when specifying $last_modified_time not worth it, as they are of no benefit to SEO.

As I wrote, I can't tell you exactly how to automate the last modified date of a page, but I'll tell you how NOT to do it!

Errors when specifying the date of the last modification

The first thing that may come to mind for most people is to send the date of the last modification of the file with the contents of the page in the header. Personally, my texts of articles are in files, not in a database, so for me this method might seem like a great way out so as not to enter every time Unix Time Stamp manually. But no! Most hosts, and maybe even all, take the date of its creation as the date of the last modification of the file, they do not take into account subsequent changes to it.

I think you understand the consequences in such cases. One popular Ukrainian hosting provider (and I think he is not the only one) in his FAQ writes something like: “Instead of the date of the last modification of the file, use the function time(), which returns the current time in Unix time stamp format." That's so absurd! It just shoots on the spot! And this hosting provider is considered “one of the best”, after I read this, I immediately did not want to become their client.

It's just anti-SEO, think for yourself, comes to your search engine page and looks: “Wow! The last time the page was changed was just now, so I guessed when to come, class! A couple of days later, he comes to the same page: “Look, it just changed again, this is a coincidence ... Wait, why don’t I see any changes? Okay, I'll come back another time." He comes again: “Well, no, guys, this is no longer funny, you definitely can’t be trusted.” Here's a fairy tale :)

And then people wonder why the results in the search results are not as they would like, but because the banal is lost to your site confidence(trust). Just like in the parable "About the shepherd and the wolves."

So, we figured out the main mistakes: you can’t specify the current time and I don’t advise you to specify the file modification time. Now let's continue to understand how it all works.

Set up sending headers Last Modified this is exactly 1/3 of the case, we still have to: make an answer to the request and turn on page caching. Both of these actions will not take much time and lines of code.

is a client request to your server, in which the client asks: “has the page changed since my last visit?”. If the page has not changed, then we must stop the execution of further loading of the page with the command:

At the same time, the body of the page should not begin to be drawn, this all happens BEFORE the first output of anything on the page! Along with this, it is necessary to return the server response to the client 304 Not Modified, thereby saying that the page needs to be taken from the cache. Let's get straight to the point:

If (isset($_SERVER["HTTP_IF_MODIFIED_SINCE"]) && strtotime($_SERVER["HTTP_IF_MODIFIED_SINCE"]) >= $last_modified_time)( header("HTTP/1.1 304 Not Modified"); die; ) header("Last-Modified : ".gmdate("D, d M Y H:i:s", $last_modified_time)." GMT");

So, in the first line, we check with the help whether the HTTP_IF_MODIFIED_SINCE request came to our server, and also immediately check the number of seconds in the incoming HTTP_IF_MODIFIED_SINCE is greater than in $last_modified_time or not? If more, then the date of the last visit of the client is later than the date of the last change of the page, from here we draw a purely logical conclusion that the page has not changed, which means that we send the server response in the second line 304 Not Modified and line 3 we kill (stop) the execution of all scripts on the page. In other words, we stop downloading it.

If the client did not send us an HTTP_IF_MODIFIED_SINCE request or his last visit was earlier than the date the page was last modified, then we (by default) return the code 200 OK and in the fifth line we send him the ACTUAL date of the page change, instead of the one he had.

About IF_MODIFIED_SINCE and how the code is arranged, he told you everything you need, except for what the strtotime () function does:

Strtotime($_SERVER["HTTP_IF_MODIFIED_SINCE"])

An attentive and savvy reader could already guess that this function converts a regular date into a Unix time stamp, since we set the $last_modified_time variable in it, and therefore, for comparison, we need to bring everything to a common denominator common system measurements.

And finally, we just have to enable caching, this is done using the following lines:

Header("CacheControl: public"); header("Expires: " . date("r", time()+10800));

Where the number 10800 is the time (in seconds) for which we want to cache the page, that is, in this example for 3 hours.

And as always, for those who do not understand anything, I post everything in full, as it is arranged on my blog:

= $last_modified_time)( header("HTTP/1.1 304 Not Modified"); die; /* kill everything below */) header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_modified_time)." GMT"); ?> And the rest of the page went

I think you may have noticed that this whole story with Last-modified is an analogue of the tag in - . So lastmod is of a familiarization and advisory nature, and no one will argue with the answers of your server. Naturally, it's not uncommon for the lastmod in the sitemap to be different from the LastModified header, but from now on, they should be the same for you! After all, now we have studied some kind of science, not in order to become like unfortunate webmasters who have not advanced further than sitemap.xml.

Personally I am in this moment I don't use the lastmod tag at all in my sitemaps, maybe I'll reconsider later, but for now I don't see the point in being so meticulous with proper titles Last Modified :)

And finally, check the correctness Last Modified and you can with this service: click .

Thank you for your attention, and special thanks to the ever-growing number of subscribers, for me this is the greatest incentive to write more often on the blog. So if you haven't signed up for new articles yet, welcome!

Last-Modified and If-Modified-Since headers for WordPress

Few pay attention to HTTP headers Last Modified And If-Modified-Since when optimizing your site, but in vain! It is important that the page, the content of which has not changed since the last visit of the search robot, gives a 304 code, which actually indicates that this particular page was not supplemented with anything - you did not edit or supplement the text, comments were not added to this entry, etc. P.

If this http header is missing, then in Yandex, when sorting the results by date, the site will not be visible to most users.

That is why it is important that you not only set it up correctly, but every time you edit a post, update the date to the current one. This will need to be done manually.

It’s easier with comments: when a visitor adds a comment, then in a variable $last_modified_time the time when the comment was added is entered automatically - this will be the date the page was last modified.

Why are the Last-Modified and If-Modified-Since headers needed?

1. When the server returns such code, the execution of all PHP scripts on the page is not even started. The page is loaded from the search cache, and this, as you understand, significantly reduces the load on the server to the great joy of your hoster and speeds up the page loading for the visitor, which also cannot but rejoice.

How does this happen?

When scanning the Internet, Google and Yandex spiders store a copy of each site in their database. This copy serves as a kind of model for comparison: whether everything is the same or whether there have been changes. And if the Last-Modified and If-Modified-Since headers are not configured or configured incorrectly, new pages of the site are indexed, and the main page in the search engine cache is not updated for a long time, just as the comment feed is not updated.

But for frequently updated pages ( news feeds, updated many times a day, actively commented blogs, etc.) it has one drawback: the information in the cache becomes outdated too quickly and a person, even reloading the page, does not see the latest news, does not see new comments. But it's still half the trouble. The trouble is that the robot does not see this either, unless the correct Last-Modified header is included.

header("Last-Modified: ".gmdate("D, d M Y H:i:s ")."GMT");

If your site is updated frequently (for example, your posts are often commented on), you can disable caching with the following set of headers:

header("Expires: ".gmdate("D, d M Y H:i:s", time() + 7200)." GMT");

This means that the validity of the stored copy must be rechecked on each request.

How does browser caching work?

If it is not disabled by calling the no_cache function, then in Firefox and IE the page is stored in the cache, and it is the page that is returned on all subsequent requests.

To refresh the page and get its latest version, you need to press the key combination Ctrl+F5, the regular Refresh button (F5) does not work. And I must say, documents in the IE cache can be stored for a very, very long time.

In Opera, the cache page is cleared by pressing the Refresh button or pressing F5. CRTL + F5 in Opera - restart all open tabs As you understand, if you have opened a lot of them - in the process of waiting, you may grow a beard.

If you disable page caching with the no_cache function, then Opera and Firefox use a mechanism with the If-Modified-Since header when accessing such a page. Thus, caching occurs, but the browser asks the server whether the page has actually changed or not - this is the correct question.

Therefore, you need to connect the processing of this parameter as well. I will not describe what and what function means, I will simply give the code that correctly returns the headers and does not cause conflicts on most hostings with which I had to work. This design works for sweb.ru, eomy.net, timeweb.ru, fastvps.ru, startlogic.com

header("Expires: ".gmdate("D, d M Y H:i:s", time() + 7200)." GMT");
header("Cache-Control: no-cache, must-revalidate");
$mt = filemtime($file_name);
$mt_str = gmdate("D, d M Y H:i:s ")."GMT";
if (isset($_SERVER["HTTP_IF_MODIFIED_SINCE"]) &&
strtotime($_SERVER["HTTP_IF_MODIFIED_SINCE"]) >= $mt)
(header("HTTP/1.1 304 Not Modified");
die;
}
header("Last-Modified: ".$mt_str);
echo $text;
header("Vary: Accept-Encoding");
header("Accept-Encoding:gzip,deflate,sdch");
?>

So all you have to do is copy this code and add it to the file header.php Your theme ABOVE . Those. this code is at the very top of the file BEFORE the rest of the code


Attention! Before adding anything, save this file on your computer so that you can restore the original version if yours does not allow such a header configuration.

We check the result on the service for checking the headers Last-Modified and If-Modified-Since http://last-modified.com/ru/if-modified-since.html


  • If the result is positive, we wipe the sweat from the forehead and go to drink tea.
  • If the result is negative, the same construct can be added to the file index.php at the root of your WordPress (I encountered this on timeweb.ru hosting). Likewise, above everything else in it. Just don't forget about it when you update - the index file will be overwritten in its standard form.

Voila! By properly setting the Last-Modified and If-Modified-Since headers, we got a bunch of bonuses:

  • Increased page loading speed, which is important for Googlebot and pleasant for people.
  • We reduced the load on the server, which pleased the hoster.
  • Yandex search results will display the date of the last page update, which in individual cases very relevant for people, and therefore indirectly it will have a positive impact on behavioral factors.
  • The pages of our site will participate in the sorting of search engines by date - yes, advanced users use this.
  • And, as a consequence of all of the above, the indexing of our site by search engines will be very accelerated.

Syntax

If-Modified-Since: , ::GMT

Directives

One of "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", or "Sun" (case-sensitive). 2 digit day number, e.g. "04" or "23". One of "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ( case sensitive). 4 digit year number, e.g. "1990" or "2016". 2 digit hour number, e.g. "09" or "23". 2 digit minute number, e.g. "04" or "59". 2 digit second number, e.g. "04" or "59". GMT

Greenwich Mean Time. HTTP dates are always expressed in GMT, never in local time.

Examples

If-Modified-Since: Wed, 21 Oct 2015 07:28:00 GMT

Specifications

Specification Title
RFC 7232, section 3.3: If-Modified-Since Hypertext Transfer Protocol (HTTP/1.1): Conditional Requests

Browser compatibility

The compatibility table in this page is generated from structured data. If you"d like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.

Update compatibility data on GitHub

DesktopMobile
ChromeedgeFirefoxInternet ExplorerOperasafariandroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
If-Modified-SinceChrome Full supportYesEdge Full support 12Firefox Full supportYesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes

The Last-Modified HTTP header tells the client when the page (object) was last modified. If the client (browser, search robot) received the Last-Modified header, then the next time it accesses the address, provided that the page (object) is in the local cache, it will add the If-Modified-Since question (whether the page has changed since the date received in Last-Modified). In turn, the server, having received an If-Modified-Since request, must check the received timestamp with the time the page was last modified and, if the page has not changed, respond with 304 Not Modified.

Save Traffic

If the page has not changed, then the server will stop transmitting data after sending headers with the 304 Not Modified code, the page body, images and other objects will not be transmitted.

Reduced server load

The correct implementation of checking the time of the last modification of the page can significantly (up to 30% or more) reduce the load on the server. Correct implementation means checking the time before the page generation starts on a dynamic site. In this case, all actions for generating the page (requesting content from the database, parsing templates, receiving comments, etc.) will not be performed. This is especially true for sites with high traffic and a long duration of the user's visit. Example: A user is on a sports news site and is constantly refreshing the homepage in anticipation of a match result being posted. In a few minutes, a page can be requested and received dozens of times. If the Last-Modified header is given and the If-Modified-Since request is processed correctly, then the page will actually be submitted once, and all subsequent requests will receive a 304 Not Modified response.

Speed ​​up indexing by search engines

Search engines recommend sending the Last-Modified header and properly handling If-Modified-Since through the Webmaster Guidelines.

Make sure your web server supports the If-Modified-Since HTTP header. This header will allow the web server to tell Google if the site's content has changed since the last time it was crawled. Support for this function will reduce the load on throughput and costs.

Google: Webmaster Guide

Make sure your HTTP headers are correct. In particular, the content of the response that the server gives to the If-Modified-Since request is important. The Last-Modified header must return the correct date the document was last modified. Even if the server does not provide the date of the last modification of the document (Last-Modified), your site will be indexed. However, in this case, the following should be taken into account:

  • search results will not show the date next to the pages of your site;
  • when sorted by date, the site will not be visible to most users;
  • the robot will not be able to get information about whether the site page has been updated since the last indexing. And since the number of pages that the robot receives from the site in one visit is limited, changed pages will be reindexed less frequently.

If you notice an error, select a piece of text and press Ctrl + Enter
SHARE: