Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply cache policy to a specific file with .htaccess?

Currently, my cache policy looks like this:

<IfModule mod_headers.c>    
  <FilesMatch "\.(css)$">
   Header set Cache-Control "max-age=691200"
  </FilesMatch>
</IfModule>

And this caches my css files for 8 days. If I wanted to cache a specific file for a year, how would I do this? I saw this answer, so I tried doing this:

<IfModule mod_headers.c>    
  <FilesMatch "\.(css)$">
   Header set Cache-Control "max-age=691200"
  </FilesMatch>
  <FilesMatch "bootstrap\.(css)$">
   Header set Cache-Control "max-age=31536000"
  </FilesMatch>
</IfModule>

As well as this just in case apache applies rules on a first come first serve basis (<- probably an incorrect use of the phrase):

<IfModule mod_headers.c>    
  <FilesMatch "bootstrap\.(css)$">
   Header set Cache-Control "max-age=31536000"
  </FilesMatch>
  <FilesMatch "\.(css)$">
   Header set Cache-Control "max-age=691200"
  </FilesMatch>
</IfModule>

But when I run the page through page speed insights, the cache policy of the bootstrap.css file remains the same. I've also cleared my own cache, opened an incognito tab, and checked the cache policy inside the network tab of the dev tools and the cache policy for the bootstrap file is still 8 days.

like image 677
John R Perry Avatar asked Feb 16 '19 03:02

John R Perry


People also ask

Are htaccess files cached?

Here is described page caching sites into the browser cache on file storage – mostly on the local hard drive of the computer. Then there is no loading of resources from the WWW server (images, JavaScript, CSS, etc.), but uses the browser cache.

How do you use cache-control max age?

Cache-Control: max-age=<seconds> This directive tells the browser or intermediary cache how long the response can be used from the time it was requested. A max-age of 3600 means the response can be used for the next 60 minutes before it needs to fetch a new response from the origin server.

What is the default value for cache policy?

The suggested default value is 32768.


2 Answers

<Files> and <FilesMatch> sections are processed in the order they appear in the configuration files, which means that the last one applied will take precedence, so your first try should work:

<IfModule mod_headers.c>    
  <FilesMatch "\.(css)$">
   Header set Cache-Control "max-age=691200"
  </FilesMatch>
  <FilesMatch "bootstrap\.(css)$">
   Header set Cache-Control "max-age=31536000"
  </FilesMatch>
</IfModule>

I have tested it and it works as expected:

$ curl -s -v example.com/bootstrap.css 2>&1 | grep Cache-Control
< Cache-Control: max-age=31536000
$ curl -s -v example.com/foo.css 2>&1 | grep Cache-Control
< Cache-Control: max-age=691200
like image 167
Dusan Bajic Avatar answered Nov 05 '22 09:11

Dusan Bajic


There are two ways you can achieve this. One is that you mentioned here. Actually max-age is a parameter which tells the browser after how many seconds it should expire. So you can calculate the number of seconds for 1 year which will be "31557600"

Another simple way is to achieve this:

<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
like image 3
Neeraj Kumar Avatar answered Nov 05 '22 07:11

Neeraj Kumar