views:

70

answers:

3

I want my CSS and JS files to expire in the distant future.

Can I do this with .htaccess alone?

How would I do it?

I know in the future if I change one I will need to force a redownload, something like this should work

script.js?v=12
+2  A: 

<FilesMatch "\.(js|css|)$">
Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
</FilesMatch>

That should do it for you. You can obviously adjust the date and time for whenever. Just remember, you need to change the name of the file if you update it.

Jorbin
Thanks for your answer. It doesn't look like you can, but is it possible to generate an expiry say 1 year from today, or is PHP necessary for that?
alex
You could set a cron to update your .htaccess file on a daily basis, but that would require you leaving your .htaccess writable.
Jorbin
+1  A: 

This uses mod_expires

<IfModule mod_expires.c>
 ExpiresActive On
 ExpiresByType text/css "now plus 1 year"
 ExpiresByType text/javascript "now plus 1 year"
</IfModule>
K Prime
+1  A: 

I use something like this:

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

age in seconds of course :))

Marek