Hi, Natalya,
Currently, our cache plugin doesn’t have the ability to enable “If-Modified-Since”.
Regards.
Hi, @tenkstars. Then I have a question. When I enable “If-Modified-Since” via .htaccess, the page code is still 200. Does the Lightspeed plugin affect the fact that the server cannot respond with ” 304 Not Modified”?
Honestly, I really don’t know. I guess you could test it by trial and error and see if it works the way you expect it to.
It seems to me that this is an important point for everyone who uses “Last-Modified” and “If-Modified-Since”. It is necessary that the page response is 304 for unchanged pages, but with the Litespeed plugin, the server response is always 200. Google also recommends using the “Last-Modified” and “If-Modified-Since” headers. Therefore, I would like to understand why the plugin removes “If-Modified-Since”, what changes the server response code and how to fix it?
Plugin Support
qtwrk
(@qtwrk)
Hi,
please correct me if I am wrong , because I just ran a test
server: cPanel + Apache + WordPress 5.7.2 , no plugin installed , Twenty Twenty theme
client: Chrome 91.0.4472.114 with private windows
I refreshed page multiple times and it always give me 200 OK status.
Best regards,
Sorry, I use a translator to communicate. Perhaps you didn’t understand me. I will try to explain the situation.
I need the server response to be 304 if the resource has not been changed.
If we checking the site for the Last-Modified header on the resource https://last-modified.com/en, it informs us that the site gave the time of the last modification, but did not respond to “If-Modified-Since”.
Since it is not yet possible to enable the “If-Modified-Since” header in the Litespeed plugin, I added the following code manually:
<?php
$LastModified_unix = strtotime(date("D, d M Y H:i:s", filectime($_SERVER['SCRIPT_FILENAME'])));
$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);
?>
After that, we test the site again, but the result has not changed. The pages will still be without “If-Modified-Since” and the server response code will be 200, despite the fact that the time of the Last-Modified has not changed.
This is probably because the “Last-Modified” header on the page is generated by the Litespeed plugin, and not by the server, so the server cannot give the “If-Modified-Since” header.
Plugin Support
qtwrk
(@qtwrk)
Hi,
I understood the question.
what I was trying to say is that , the WordPress default doesn’t have such behavior.
and from what I can tell , and reading from source code , LiteSpeed Cache plugin does NOT touch anything regard Last-Modified
header.
if site doesn’t have it, then LSCWP won’t add it.
if site has it , then LSCWP won’t touch it.
It respects the header generated by server/PHP
and it also requires browser to send out If-Modified-Since
header.
Did you make it work on default WordPress without any cache plugin first ?
Best regards,
Yes, by default, WP does not include various headers. So I added the above code, which adds the If-Modified-Since and Last-Modified headers. Before switching to the server with Litespeed, I used the “Cache Enabler” plugin, and it automatically added these headers. It would be great if it was possible and Litespeed, as this speeds up the indexing of new pages, and also reduces the load on the server because Google, Yandex, Bing will get a response 304 unmodified pages and they do not need to re-scan the page.
Plugin Support
qtwrk
(@qtwrk)
Hi,
may I have steps for how to achieve this on an Apache server with default WP installation ?
I will do some check and test around
Best regards,
Before writing to you, I turned to my hosting with a question about how to enable “If-Modified-Since”. They provided me with this code that needs to be inserted into header.php before the html tag.
<?php
$LastModified_unix = strtotime(date("D, d M Y H:i:s", filectime($_SERVER['SCRIPT_FILENAME'])));
$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);
?>
In addition, there is another way, this code must be inserted into the file .htaccess.
RewriteEngine on
RewriteRule .* - [E=HTTP_IF_MODIFIED_SINCE:%{HTTP:If-Modified-Since}]
RewriteRule .* - [E=HTTP_IF_NONE_MATCH:%{HTTP:If-None-Match}]
When I told them that this method does not work for me, they said that they installed a clean WP on their test site, inserted 1 version of the code and everything worked for them. After that, they advised me to contact the developers.
You can test both methods. Also for testing, please pay attention to the plugin Cache-Enabler, it has support for 304 Not Modified.
Maybe I misunderstood, but it says here that the Litespeed server sets the response header “Last-Modified” – litespeedtech.com. Then where is “If-Modified-Since”…
Plugin Support
qtwrk
(@qtwrk)
Hi,
no , because the litespeedtech.com site is on Joomla, not wordpress , https://blog.litespeedtech.com is
both of your methods don’t work on me , did I miss any steps ?
do you have working demo site for me to check ?
Best regards,
Hi! I can’t provide you with a test site on this server yet. Most likely, I will buy a new domain and create a new website, but for this I need some time. Have you tested without the LS plugin? It may be possible to try to disable file compression in CPANEL, sometimes “-gzip” or “- br” can be added to the contents of the “Last-Modified” and “ETag”headers. Also try creating an empty file in the root folder test.php and insert 1 version of the php code into it. Open it in the browser and check the headers of this page, do not forget to remove the check mark from the “Disable cache”item in the developer tools. I read somewhere that the “Content-Length” header should also be present.
Also, please pay attention to the Litespeed documentation, it just talks about 304 Not Modified. Just how to make it work?
Plugin Support
qtwrk
(@qtwrk)
Hi,
yes, all my test was done without any plugin
cPanel + Apache + WP 5.7.2 , with default theme , no plugin installed/activated
by empty root with test.php and insert it , it isn’t valid for WP case
no , that is standalone documentation , may not apply to WordPress
like I have explain in previously , the cache plugin would honor the default WP behavior as much as possible , by that saying ,
1. if WP itself doesn’t send this , then we don’t add it
2. if WP itself sends this , then we don’t touch it
3. if you make it work on WP without cache plugin , and then it breaks with cache plugin enabled , then we will investigate
Best regards,
Friends, I just disabled the LS plugin on one site and 304 Not Modified immediately started working. (screenshot of the site without the plugin) Whereas on the second site with the plugin enabled, the test result is negative. (screenshot of the website with the plugin). I’m sorry that I’m sending you screenshots in Russian, since I didn’t find a similar testing tool, but the response code is specified in English.