How To add Mod Expires To your .htaccess

How to Add mod expires To Your .htaccess

Instead of waiting for every website visit to cache in your browser, control can be exercised through cache control HTTP headers by adding a mod-expire.Mod-expires hold the files(images,texts,jpeg,flash etc) with a duration of time on how each should expire. This is done by editing .htaccess with a code that defines expiry time, so that whenever requests are made to the server, the load time is fast and files dont have qeue when requested. Then a date must be set so that when the cache expires its deleted from the browser. The duration maybe a day,minute,second,hourly or even yearly. Usually, there are three directives that can be set to expire and they are explained as below

  • Expiresactive directive- enables expiry of headers. All files expire at the same duration.
  • Expiresbytype directive- enables type of file and expiry depending on how old it is
  • Expiresdefault directive- this directive sets age for every document except the ones specified by expiresbytype.

To add a mode expire;

  • Log in to Cpanel
  • Click file manager
  • look for public-html- then .htaccess
  • Right click .htaccess to edit and in the text editor add the following code
    
    
    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"

For every content specified here, for instance image/jpeg will expire at its own time, different from image/jpg.

For files that take long to be updated such as images the mode-expires can be set to keep them longer unlike for text files that can require update oftenly.

Was this article helpful?

Related Articles

Leave A Comment?