Question

How can I cache the image on my website for 2 days with the htaccess so:

1 x 60 x 60 x 24 x 2 = 172800s

So I want to cache 'png, jpeg, jpg, ico, js'. How can I do this with the htaccess

Was it helpful?

Solution

You can use the mod_expires module (based in the MIME type of the file):

<ifModule mod_expires.c>
ExpiresActive On
ExpiresDefault A300

# png, jpeg, jpg, ico, js expire after 2 days
ExpiresByType image/gif A172800
ExpiresByType image/png A172800
ExpiresByType image/jpg A172800
ExpiresByType image/x-icon A172800
ExpiresByType application/x-javascript A172800
</ifModule>

Or the mod_headers module (based in file extension):

<ifModule mod_headers.c>
ExpiresActive On

# png, jpeg, jpg, ico, js expire after 2 days
<filesMatch ".(gif|png|jpg|jpeg|ico|js)$">
Header set Cache-Control "max-age=172800"
</filesMatch>
</ifModule>

This last one gives you more options, like force no-cache, etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top