alex
alex

Reputation: 490233

How can I use .htaccess to send distant expiry headers on CSS and JS files?

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

Upvotes: 0

Views: 1420

Answers (3)

Marek
Marek

Reputation: 2731

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 :))

Upvotes: 1

K Prime
K Prime

Reputation: 5849

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>

Upvotes: 2

Jorbin
Jorbin

Reputation: 171

<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.

Upvotes: 2

Related Questions