Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP cache headers with .htaccess

I am trying to configure my .htaccess file to set the cache time. Tryied every possible configuration but nothing works!

This is what is written in my HTML:

 <meta http-equiv="Cache-Control" content="max-age=2592000, public" />
 <!--        
 <meta http-equiv="expires" content="mon, 24 sep 2012 14:30:00 GMT">
  -->

and this is what written in my .htaccess file:

ExpiresActive On
ExpiresDefault A3600

However, when I refresh inclusind cache clear (ctrl+F5) in firefox, my firebug NET panel says that the cache expires at the same second I have accessed the file ( and not in the future, as I want it to be).

What am I doing wrong??

Thanks

like image 478
Yura Avatar asked Sep 19 '12 15:09

Yura


People also ask

Does htaccess get cached?

htaccess file? Their contents are not cached, they are read on every request. it's a file called . htaccess, in the directory /var/www.

How do I set HTTP headers for cache control?

To use cache-control in HTML, you use the meta tag, e.g. The value in the content field is defined as one of the four values below. HTTP 1.1. Allowed values = PUBLIC | PRIVATE | NO-CACHE | NO-STORE.

Do HTTP headers get cached?

Cache-control is an HTTP header used to specify browser caching policies in both client requests and server responses. Policies include how a resource is cached, where it's cached and its maximum age before expiring (i.e., time to live).


1 Answers

I advise you to use headers mod. You can activate it (if disabled) with this command :

a2enmod headers

Here is a simple code example that works:

<IfModule mod_headers.c>
    # WEEK
    <FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
        Header set Cache-Control "max-age=604800, public"
    </FilesMatch>

    # WEEK
    <FilesMatch "\.(js|css|swf)$">
        Header set Cache-Control "max-age=604800"
    </FilesMatch>
</IfModule>

max-age is cached time in seconds.

like image 148
Maraumax Avatar answered Sep 27 '22 15:09

Maraumax