Apache htaccess: Add Cache-Control for Browser Caching

Browser Caching is the fastest and easy way to improve website speed and load time. Firstly, Web Browser Check the headers of every requested web page. If caching control present in headers then browser will store web page and its files into local memory of your device. Next time when you access same web page then browser restore it from the local memory rather than requesting it to server.

To Control Browser caching you must be enabled it from your hosting panel or contact your developer. In this article we'll tell you how can you control browser caching in apache server using ".htaccess" file.

How to Create/Edit ".htaccess" file?

  1. Open your hosting control panel
  2. Goto File Manager or Access your files via FTP
  3. Navigate to "public_html" directory/folder
  4. Find ".htaccess" file or create a new blank text file with the name of ".htaccess"
  5. Now, Edit this File and Put following codes at the end of file.

Setting an expiry date or a maximum age in the HTTP headers for static resources instructs the browser to load previously downloaded resources from local disk rather than over the network.

RewriteEngine on
## Caching Configuration by https://www.weforit.com ##
## START EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
## SET Caching for CSS ##
ExpiresByType text/css "access 1 month"
</IfModule>
## END EXPIRES CACHING ##
RewriteEngine on
## Caching Configuration by https://www.weforit.com ##
## START EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
## SET Caching for JavaScript file ##
ExpiresByType text/x-javascript "access 1 month"
</IfModule>
## END EXPIRES CACHING ##
RewriteEngine on
## Caching Configuration by https://www.weforit.com ##
## START EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
## SET Caching for Images ##
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
</IfModule>
## END EXPIRES CACHING ##
RewriteEngine on
## Caching Configuration by https://www.weforit.com ##
## START EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
## SET Caching for Favicon ##
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
## END EXPIRES CACHING ##
RewriteEngine on
## Caching Configuration by https://www.weforit.com ##
## START EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
## SET Caching for PDF file ##
ExpiresByType application/pdf "access 1 month"
</IfModule>
## END EXPIRES CACHING ##
RewriteEngine on
## Caching Configuration by https://www.weforit.com ##
## START EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
## SET Caching for Flash Files ##
ExpiresByType application/x-shockwave-flash "access 1 month"
</IfModule>
## END EXPIRES CACHING ##
RewriteEngine on
## APACHE htaccess CACHE CONTROL by www.weforit.com ##
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
## SET Caching for Images ##
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"

## SET Caching for CSS ##
ExpiresByType text/css "access 1 month"

## SET Caching for PDF file ##
ExpiresByType application/pdf "access 1 month"

## SET Caching for JavaScript file ##
ExpiresByType text/x-javascript "access 1 month"

## SET Caching for Flash Files ##
ExpiresByType application/x-shockwave-flash "access 1 month"

## SET Caching for Favicon ##
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
## EXPIRES CACHING ##

Compressing resources with gzip or deflate can reduce the number of bytes sent over the network.

## OPTION 1 ##
## APACHE htaccess CACHE CONTROL by www.weforit.com ##
## Please try another method if it is not working ##
<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>
## OPTION 2 ##
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

This article is under writing

Comments

WeFor IT

Social Links