4 Replies - 3042 Views - Last Post: 27 September 2012 - 06:10 AM

#1 xYassineHoussni  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 9
  • Joined: 21-September 12

.htaccess cache-control doesn't cache my files (js|png|css etc.)

Posted 27 September 2012 - 02:48 AM

As my title says, I am trying to cache-control the content on my website but it seems that my content isn't being cached.

This is my code in my .htaccess file:

Header unset Pragma
FileETag None
Header unset ETag

# cache images/pdf docs for 10 days
<FilesMatch "\.(ico|pdf|jpg|jpeg|png|gif|js)$">
Header set Cache-Control "max-age=864000, public, must-revalidate"
Header unset Last-Modified
</FilesMatch>

# cache html/htm/xml/txt diles for 2 days
<FilesMatch "\.(html|htm|xml|txt|xsl)$">
Header set Cache-Control "max-age=7200, must-revalidate"
</FilesMatch>


Link to my website if needed: http://www.awesomephp.nl/

I have tried a lot of tutorials but none of them seems to do the trick. By the way, other .htaccess codes, like redirecting and stuff, DOES work.

Is This A Good Question/Topic? 0
  • +

Replies To: .htaccess cache-control doesn't cache my files (js|png|css etc.)

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: .htaccess cache-control doesn't cache my files (js|png|css etc.)

Posted 27 September 2012 - 04:21 AM

Hey.

Why are you unsetting the Last-Modified header? Seems like an odd thing to do if you want the content cached.

I tried this on my test server, and with the Last-Modified header removed, like it is in your code, all the specified file types are downloaded on each request. When the Header unset Last-Modified header is removed, however, all files are cached as expected. (Tested with Firefox/Firebug and Chrome.)
Was This Post Helpful? 1
  • +
  • -

#3 xYassineHoussni  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 9
  • Joined: 21-September 12

Re: .htaccess cache-control doesn't cache my files (js|png|css etc.)

Posted 27 September 2012 - 04:42 AM

View PostAtli, on 27 September 2012 - 11:21 AM, said:

Hey.

Why are you unsetting the Last-Modified header? Seems like an odd thing to do if you want the content cached.

I tried this on my test server, and with the Last-Modified header removed, like it is in your code, all the specified file types are downloaded on each request. When the Header unset Last-Modified header is removed, however, all files are cached as expected. (Tested with Firefox/Firebug and Chrome.)


How did you check the cache with firebug?
And is it possible that the problem is at my serverconfiguration?
Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: .htaccess cache-control doesn't cache my files (js|png|css etc.)

Posted 27 September 2012 - 05:55 AM

View PostxYassineHoussni, on 27 September 2012 - 11:42 AM, said:

How did you check the cache with firebug?

You can see things like that in the "Net" tab.

For instance, with your original config for images and JS files:
# cache images/pdf docs for 10 days
<FilesMatch "\.(ico|pdf|jpg|jpeg|png|gif|js)$">
    Header set Cache-Control "max-age=864000, public, must-revalidate"
    Header unset Last-Modified
</FilesMatch>


Firebug shows me this on my test page for every request I make

Attached Image

However if I comment out or remove the Header unset Last-Modified line, then I only get the above for the first request. After that I just get this:

Attached Image

Notice how the JS and JPEG files now have a greyed out 304 status code. That indicates it's being loaded from the cache. (You can also click the files to see more info relating to how it's cached.) Also notice the "from cache" line in the grey bottom bar. In the second image, it shows that pretty much all the data is being loaded from the cache.

View PostxYassineHoussni, on 27 September 2012 - 11:42 AM, said:

And is it possible that the problem is at my serverconfiguration?

It's possible. It may not have the mod_headers loaded (although that should show a 500 status if you try to use it), or it may not allow you to use the Header directive in a .htaccess file. According to the docs, the AllowOverride directive for the host must include FileInfo (or just All). You should make sure that is the case.
Was This Post Helpful? 1
  • +
  • -

#5 xYassineHoussni  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 9
  • Joined: 21-September 12

Re: .htaccess cache-control doesn't cache my files (js|png|css etc.)

Posted 27 September 2012 - 06:10 AM

View PostAtli, on 27 September 2012 - 12:55 PM, said:

View PostxYassineHoussni, on 27 September 2012 - 11:42 AM, said:

How did you check the cache with firebug?

You can see things like that in the "Net" tab.

For instance, with your original config for images and JS files:
# cache images/pdf docs for 10 days
<FilesMatch "\.(ico|pdf|jpg|jpeg|png|gif|js)$">
    Header set Cache-Control "max-age=864000, public, must-revalidate"
    Header unset Last-Modified
</FilesMatch>


Firebug shows me this on my test page for every request I make

Attachment fbcache1.png

However if I comment out or remove the Header unset Last-Modified line, then I only get the above for the first request. After that I just get this:

Attachment fbcache2.png

Notice how the JS and JPEG files now have a greyed out 304 status code. That indicates it's being loaded from the cache. (You can also click the files to see more info relating to how it's cached.) Also notice the "from cache" line in the grey bottom bar. In the second image, it shows that pretty much all the data is being loaded from the cache.

View PostxYassineHoussni, on 27 September 2012 - 11:42 AM, said:

And is it possible that the problem is at my serverconfiguration?

It's possible. It may not have the mod_headers loaded (although that should show a 500 status if you try to use it), or it may not allow you to use the Header directive in a .htaccess file. According to the docs, the AllowOverride directive for the host must include FileInfo (or just All). You should make sure that is the case.


Thanks for all the help!! Everything worked and I know now how firebug works now and I understand some more about the caching, thank you very much for your time!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1