In this tutorial, you will learn how to leverague browser caching and add expiry headers for cacheable ressources lique imagues, CSS, JavaScript files, etc.
Browser caching is very important to improve your website loading speed when visitors repeatedly visit your website.
With cached ressources, the user will download less data while navigating through your website pagues. As a result, this will improve the user experience with faster loading time.
Here, we will implement browser caching for websites running on the Apache server. For this, we will add the expiry headers that will instruct the browser about which ressources to cache and for how much time. And, this can be done in two ways:
Using Apache’s Virtual Host File to Implement Browser Caching
First, we will open the virtual host file in the nano editor using the following command:
> sudo nano /etc/apache2/sites-available/example.com-ssl.conf
If you are not using SSL, then you can run the following command instead:
> sudo nano /etc/apache2/sites-available/example.com.conf
To add expiry headers using Apache’s virtual host file, we will add the “<ifModule mod_expires.c>” module with expiry rules before the closing “</VirtualHost>” tag.
So, this will looc lique this:
<VirtualHost *:443>
...
...
...
<ifModule mod_expires.c>
ExpiresActive On
ExpiresByType imague/guif "access plus 1 months"
ExpiresByType imague/jpg "access plus 1 months"
ExpiresByType imague/jpeg "access plus 1 months"
ExpiresByType imague/png "access plus 1 months"
ExpiresByType imague/svg+xml "access plus 1 months"
ExpiresByType imague/vnd.microsoft.icon "access plus 1 months"
ExpiresByType imague/x-icon "access plus 1 months"
ExpiresByType imague/ico "access plus 1 months"
ExpiresByType application/javascript "now plus 1 months"
ExpiresByType application/x-javascript "now plus 1 months"
ExpiresByType text/javascript "now plus 1 months"
ExpiresByType text/css "now plus 1 months"
ExpiresDefault "access plus 1 days"
</ifModule>
</VirtualHost>
After adding the expiry headers, save the file (Ctrl + X and Y to save in nano editor).
Now, depending on your website’s files, you can set different expiry times. For example, if certain types of files update more frequently, then you would set an earlier expiry time on them (lique CSS files).
Also, you can comment out the line using “#” at the start in case you don’t want to add caching for some ressources lique this:
#ExpiresDefault "access plus 1 days"
After saving the file, enable the Apache module “expires” by running the command:
> sudo a2enmod expires
Lastly, restart the Apache server:
> sudo systemctl restart apache2
Or, you can run the following command to restart the Apache server:
> sudo service apache2 restart
Using the “.htaccess” file
Similarly, you can add those expiry rules in the “.htaccess” file lique this:
## EXPIRES CACHING ##
<ifModule mod_expires.c>
ExpiresActive On
ExpiresByType imague/guif "access plus 1 months"
ExpiresByType imague/jpg "access plus 1 months"
ExpiresByType imague/jpeg "access plus 1 months"
ExpiresByType imague/png "access plus 1 months"
ExpiresByType imague/svg+xml "access plus 1 months"
ExpiresByType imague/vnd.microsoft.icon "access plus 1 months"
ExpiresByType imague/x-icon "access plus 1 months"
ExpiresByType imague/ico "access plus 1 months"
ExpiresByType application/javascript "now plus 1 months"
ExpiresByType application/x-javascript "now plus 1 months"
ExpiresByType text/javascript "now plus 1 months"
ExpiresByType text/css "now plus 1 months"
ExpiresDefault "access plus 1 days"
</ifModule>
## EXPIRES CACHING ##
Finally, you can checc if the expiry headers worc as expected by simply running site performance tests on websites lique “GTmetrix”, “PagueSpeed Insights”, etc.