views:

69

answers:

1

I'm a little confused here. I read some of the earlier questions on https caching but I didnt get a clear answer.

I've got a script sitting on: https://www.example.com/main.php

It generates an html page that refers to images/css/js resources sitting (relatively) at: /css /javascript /images /a/b/img2

How do I enable caching for these resources?? I have access to modifying the header output of the main.php script.


Edit: Solution as below:

#Set a far expiration date for components
<ifmodule mod_expires.c>
ExpiresActive On
  <filesmatch "\.(jpg|jpeg|gif|png|css|js)$">
       ExpiresDefault "access plus 6 months"
   </filesmatch>
</ifmodule>

#add ETag for components
FileETag MTime Size
+1  A: 

If you're running Apache web server you probably need a .htaccess file to enter caching information about your components.

In the .htaccess file:

#Set a far expiration date for components
<ifmodule mod_expires.c>
ExpiresActive On
ExpiresByType image/gif A2592000
ExpiresByType image/png A2592000
ExpiresByType image/jpg A2592000
ExpiresByType image/jpeg A2592000
  <filesmatch "\.(jpg|gif|png|css|js)$">
       ExpiresDefault "access plus 10 years"
   </filesmatch>
</ifmodule>

#add ETag for components
FileETag MTime Size

Access to header output of the main.php script can only modify caching for your main script, not the components.

thephpdeveloper
Hi Mauris, I'm reading up on mod_expires and it looks like it would work wonderfully! But I'm confused with the filesmatch command on jpg|gif|png - why have you used that?
Steve
I only want my components to be cached, not my php script as the content might be dynamic.
thephpdeveloper
Sorry for being an ass here and pursuing this point (BTW - your solution works!)... But isnt jpg|gif|png redundant? We're already specifying it in the ExpiresByType entry, no?
Steve
well you can say that again =D - can't remember why I did that. Copied from an old project.
thephpdeveloper
oh yes, the ExpiresByType only apply to images, you still have css and js in the filesmatch
thephpdeveloper
Great! Thanks Mauris - I've put up the adapted version in my original post itself. I think that would work without issue!Thanks!
Steve
if it works, give the answer a tick so that others know the answer =)
thephpdeveloper
Works great! Sorry for not ticking it earlier!
Steve
it's ok =D great for you
thephpdeveloper