Signup/Sign In
PUBLISHED ON: MARCH 9, 2023

Enable web browser caching in Apache server

Apache web caching is a way to improve website performance by storing commonly accessed content in a cache so that users can quickly retrieve it. This can significantly reduce the time it takes for a page to load, as well as reduce the load on the server by reducing the number of requests it has to process. There are several ways to enable web browser caching in Apache, including editing the .htaccess file and using the "Cache-Control" header.

Enable web browser caching - Edit .htaccess file

One way to enable web browser caching in Apache is to edit the .htaccess file. This file is located in the website's root directory and can be used to configure various aspects of the server, including caching.

To enable caching in the .htaccess file, you can use various directives, such as ExpiresDefault and ExpiresByType. For example, the following directives will enable caching of images and CSS files for a period of one month and two months respectively:

ExpiresDefault "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType text/css "access plus 2 months"
ExpiresByType text/javascript "access plus 2 months"
ExpiresByType image/jpg "access plus 5 month"
ExpiresByType image/jpeg "access plus 5 month"
ExpiresByType image/gif "access plus 5 month"
Header set Cache-Control "public, max-age=2592000"

You can also use the Cache-Control header set directive to set the Cache-Control header for your website. This header tells the web browser how long to cache content, as well as other caching-related information. For example, the following directive will set the Cache-Control header to "public, max-age=2592000", which means that the content will be cached for 30 days:

Header set Cache-Control "public, max-age=2592000"

Clear cache in user's web browser using Apache

Another way to enable web browser caching in Apache is to use the mod_headers module to force the user's web browser to clear its cache. This can be useful if you've made changes to your website and want to ensure that users see the updated version of the content. To force the user's web browser to clear the cache, you can use the following directive in the .htaccess file:

Header set Cache-Control "no-cache, no-store, must-revalidate"

This directive will set the Cache-Control header to "no-cache, no-store, must-revalidate", which tells the web browser not to cache content and to revalidate it with the server each time it's called. required.

Conclusion

Web browser caching can significantly improve the performance of a website by reducing the time it takes to load a page and reducing the load on the server. There are several ways to enable web browser caching in Apache, including editing the .htaccess file and using the Cache-Control header. By using these methods, you can improve the user experience of your website and make it more efficient.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.