Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

htaccess / mod_expires - caching specific files

Ok, I checked a lot of websites about how to manage the browser's cache memory with a .htaccess file, but it is still very not clear to me.

I want to cache specific files for one month. For the rest, I want it to be refreshed every time. So I tried:

<IfModule mod_headers.c> 
    Header unset Cookie
    Header unset Set-Cookie
    Header unset Cache-Control
    Header unset ETag
    FileETag none 
</IfModule>

<IfModule mod_expires.c>  
    ExpiresActive On
    ExpiresDefault "now"
    <Files "/css/jquery-ui.css">
      ExpiresDefault "access plus 1 month"
    </Files>
    <Files "/js/jquery-1.10.2.min.js">
      ExpiresDefault "access plus 1 month"
    </Files>
    <Files "/js/jquery-ui.js">
      ExpiresDefault "access plus 1 month"
    </Files>
    <Files "/js/analytics.js">
      ExpiresDefault "access plus 1 month"
    </Files>
    <Files "/matheos/img/*">
      ExpiresDefault "access plus 1 month"
    </Files>
    <Files "/img/*">
      ExpiresDefault "access plus 1 month"
    </Files>
</IfModule>

But it doesn't work exactly as expected...

The HTML is correctly not cached, but the specific files like jquery-ui.css, which should be cached for 1 month, are also not cached.

Anyway, does this .htaccess seem ok for you ?

like image 448
Sharcoux Avatar asked Nov 28 '14 13:11

Sharcoux


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.

What is ExpiresByType?

ExpiresByType DirectiveThis directive defines the value of the Expires header and the max-age directive of the Cache-Control header generated for documents of the specified type (e.g., text/html ). The second argument sets the number of seconds that will be added to a base time to construct the expiration date.

What is Ifmodule Mod_expires C?

Mod_expires is an Apache module that is used to manipulate the Expires HTTP header and the max-age directive of the Cache-Control header. These two HTTP headers instruct the internet browser of the client for how long should particular documents and files be cached.


1 Answers

Ok, got it ! To target a specific file, the correct syntax is :

# to not cache css except jquery-ui.css
ExpiresByType text/css "now"
<FilesMatch "jquery-ui\\.css$">
    ExpiresByType text/css "access plus 1 month"
</FilesMatch>

This is the only way that worked for me, at least in the case of an ovh shared host. I also tried all possible combinations with ExpiresDefault but it didn't work...

like image 74
Sharcoux Avatar answered Oct 28 '22 04:10

Sharcoux