Forum Moderators: phranque

Message Too Old, No Replies

.htaccess Issue

Problem with .htaccess file.

         

Cinws

8:28 am on Jun 16, 2010 (gmt 0)

10+ Year Member



Hello,

I'm trying to implement caching on my web host using .htaccess.

I've added the following code to the .htaccess file:

<ifModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 seconds"
ExpiresByType text/html "access plus 1 seconds"
ExpiresByType image/gif "access plus 2592000 seconds"
ExpiresByType image/jpeg "access plus 2592000 seconds"
ExpiresByType image/png "access plus 2592000 seconds"
ExpiresByType text/css "access plus 604800 seconds"
ExpiresByType text/javascript "access plus 216000 seconds"
ExpiresByType application/x-javascript "access plus 216000 seconds"
</ifModule>

<ifModule mod_headers.c>
<filesMatch "\\.(ico|pdf|flv|jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=2592000, public"
</filesMatch>
<filesMatch "\\.(css)$">
Header set Cache-Control "max-age=604800, public"
</filesMatch>
<filesMatch "\\.(js)$">
Header set Cache-Control "max-age=216000, private"
</filesMatch>
<filesMatch "\\.(xml|txt)$">
Header set Cache-Control "max-age=216000, public, must-revalidate"
</filesMatch>
<filesMatch "\\.(html|htm|php)$">
Header set Cache-Control "max-age=1, private, must-revalidate"
</filesMatch>
</ifModule>


But when I view the HTTP Request headers, it shows that no caching happening.

Any ideas what I'm doing wrong?

Thanks in advance.

jdMorgan

1:12 pm on Jun 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As one example,
 <filesMatch "\\.(ico|pdf|flv|jpg|jpeg|png|gif|swf)$"> 

should be
 <[b]Fi[/b]lesMatch [b]"\.([/b]ico|pdf|flv|jp[b]e?g[/b]|png|gif|swf)$"> 

Also, list the filetypes in order of most- to least-frequently-requested.

Mod_expires adds the "max-age=" parameter to the Cache-Control Header, so doing it in both mod_expires and in mod_headers is redundant.

Note that adding these headers allows/invokes client-side caching.

Do yourself a very big favor, and set your expiry/max-age time to much smaller time periods until you get this code working and have 'tested' it for a week or two. Once a client caches something (say for a month), then you are stuck: That client will not re-fetch that resource for 30 days, and in most cases, there is nothing you can do about it.

If mod_expires or mod_headers is not available on the server, your use of the <IfModule> containers will cause this code to fail silently. Be very sure that that is what you want.

Jim