This article will guide you through controlling browser caching using the .htaccess
file.
public_html
directory or the root directory of your website..htaccess
File:.htaccess
file exists, open it for editing..htaccess
.Insert the following code to set cache expiration times for various file types:
# Enable mod_expires
ExpiresActive On
# Set default expiration time to 1 month
ExpiresDefault "access plus 1 month"
# Set specific expiration times for different file types
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/javascript "access plus 1 month"
ExpiresByType application/font-woff2 "access plus 1 year"
This configuration sets the following cache expiration times:
.htaccess
File:.htaccess
file.mod_expires
module is enabled on your server. If it's not enabled, you can use the mod_headers
module to set cache control headers: