X
X

Browser Caching with .htaccess File

Adjusting browser caching via .htaccess file can increase your website's performance and improve user experience. Browser caching allows the browser to download certain files (e.g., CSS, JavaScript, images) once and store them locally, and not download them again when the same files are accessed later. Here are a few examples of setting browser caching using a .htaccess file:

Example 1: Caching all static files:
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 year"
</IfModule>
This example ensures that all static files (CSS, JavaScript, images, etc.) are cached. The ExpiresDefault directive allows browsers to cache files for one year.

Example 2: Caching certain file types:

<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType text/css "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
</IfModule>
In this example, the caching period for certain file types (images, CSS, JavaScript) is set to one month.

Example 3: Caching specific directories:
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/css "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
<Directory /path/to/your/directory>
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
</Directory>
</IfModule>
This example sets the caching time for files in a given directory differently.

Adding these codes to your .htaccess file will help you configure the browser cache. However, make sure that these files have the mod_expires module enabled on your server. I also recommend making a backup before making changes to the configuration so that you can fix any possible problems that may occur as a result of incorrect configurations.

Can't find the information you are looking for?

Create a Support Ticket
Did you find it useful?
(49 times viewed / 0 people found it helpful)
Top