Increase page loading speed – Leverage browser caching – .htaccess

How it can be done via .htaccess file?

Leverage browser caching – is a simple way to reduce the loading time for web pages for regular visitor. Particularly effective on websites where users regularly re-visit the same areas of the website. Every time a web browser loads a web page it downloads all web file to load the page properly. It includes HTML, images, CSS, scripts etc. Most of the pages include the same contents repeatedly. In this way we can really reduce the loading speed to a great extent!

Where I can enable this feature?

It’s basically an Apache module and this should be enabled by adding specific code to the Apache configuration file. Most of the users have only user level access to the server. That’s the important of this post. Here I’m listing a code which can be added to the .htaccess file to enable this feature. The .htaccess is located in the public folder of your website.

Here is the code to add:

ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"

By using above code, the main static contents such as images, CSS, PDF’s, JS, etc will be cached in web browser in order to produce faster loading times in the future.

That’s it!


1, http to https redirection using .htaccess file
2, How to disable phpinfo(); in a shared environment/hosting?

Arunlal Ashok

Linux Server Administrator. I'm dealing with Linux servers since 2012. I started this blog to share and discuss my ideas with the world. Check My Profile!! in uPwork (oDesk) and let me know if you need any assistance. Thanks!!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *