How to: Add Leverage Browser Caching in WordPress via .htaccess?

To enable browser caching on your blog, you just need to edit HTTP headers and set expiry dates on certain files. This can be done by rewriting the rules of your blog’s .htaccess file which you can find in the root of your domain. By default this file is hidden on your cPanel but can be edited using Filezilla or any other FTP clients software. You can edit the .htaccess file with notepad but take into account just save the file by pressing ctrl+s (don’t save the file as .txt).

Leverage-browser-cache

Adding Leverage Browser Caching

The common reason of editing the file is to set our caching parameters and to knock the browser what types of files to be cached and when. The recommended method of settings these parameters is to add the following code in your .htaccess file:

## EXPIRES CACHING ##

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

It depends on your blog files you can set different expiry dates too. If some types of files are updated most frequently, you can set an earlier expiry dates on that files an example of the file that most frequently edited style.css files.

Be sure to use the expiry dates on the certain files, don’t set expiry dates for the future time (i.e, 2 years or more). The expiry dates should not even very smaller and even for the future time. Because users might not be getting the fresh version of your blog after updates.

Thanks for reading.

Similar Posts

2 Comments

Leave a Reply

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