Caching improves your website speed & performance, and reduces load on your server. Last-modified header is an important HTTP header that is used for caching your website’s content on user’s web browser. You can set it in Apache web server to ensure that users always see fresh content without unnecessarily sending requests to your web server. Let’s take a look at how to set the last modified header in Apache.
How to Set the Last Modified Header in Apache
Here are the steps to set the last modified header in Apache.
Before you proceed, please ensure that you have enabled .htaccess (mod_rewrite) in Apache. If not, then here’s how you can do it:
Place your .htaccess file in the root document folder of your website (/var/www/html).
1. Open .htaccess File
Open .htaccess file in a text editor
$ sudo vim /var/www/html/.htaccess
2. Set Last Modified Header
Simply add the following line to set last modified header
Header set Last-Modified "Sun Feb 1 00:32:50 EDT 2019"
Please note, it is not advisable to delete or remove Last modified header. It might mess up caching for your website.
3. Restart Apache web server
Restart Apache web server to apply changes
$ sudo /etc/init.d/apache2 start [Debian or Ubuntu] # sudo apachectl restart [RHEL, CentOS or Fedora]
4. Test Last Modified Header
Use one of the online tools to check the values of your HTTP header “Last Modified Date”
That’s it! Now Apache will automatically update the Last Modified Date in response headers. However, be careful while changing the last modified date. This value is used by web browsers to determine if your cache content has expired or not, and whether to request fresh content to your web server. If you don’t set it properly, it might trigger a flood of requests from all your website users, and increase your server load.