4

I am not totally sure bout HTTP headers, but from what I read its good to have some level of caching on static pages also I am not sure if Transfer Encoding: chunked is a good thing. I was not finding a definite answer as to how best to run this cache with my PHP files so that caching is enabled and when content changes it should update the cache. Hopefully I can get assisted with this, I was wondering which of the following would be best to use or any advice :

header('Cache-control: public');

OR

header('Cache-control: max-age=10');

Thanks for your time.

1 Answer 1

1

In Most Cases PHP and HTML Caching is Bad

Generally it is not advisable to cache the PHP and HTML files of a site since when you modified these pages your returning visitors will not fetch the newer page without a hard refresh of the page. It is recommended By Google Insight to cache the images and other files that are unlikely to change. So when you edit images you should save them as a new file name and that way your visitors get the new files even when they have a cache of 2 weeks on media files.

Expires Via .HTACCESS

One of the best ways of controlling expires is via the .htaccess file using the Apache2 expires module.

Below is some recommended code

<IfModule mod_expires.c>
 ExpiresActive on

 ExpiresDefault      "access plus 1 month"
 ExpiresByType text/cache-manifest "access plus 0 seconds"
 #HTML PAGES
 ExpiresByType text/html "access plus 0 seconds"

 #DATA
 ExpiresByType text/xml "access plus 0 seconds"
 ExpiresByType application/xml "access plus 0 seconds"
 ExpiresByType application/json "access plus 0 seconds"

 #RSS
 ExpiresByType application/rss+xml "access plus 1 hour"

 #FAVICON (cannot be renamed)
 ExpiresByType image/x-icon "access plus 1 week"

 #MEDIA
 ExpiresByType image/gif "access plus 1 month"
 ExpiresByType image/png "access plus 1 month"
 ExpiresByType image/jpg "access plus 1 month"
 ExpiresByType image/jpeg "access plus 1 month"
 ExpiresByType video/ogg "access plus 1 month"
 ExpiresByType audio/ogg "access plus 1 month"
 ExpiresByType video/mp4 "access plus 1 month"
 ExpiresByType video/webm "access plus 1 month"

 #HTC TRICK
 ExpiresByType text/x-component "access plus 1 month"

 #WEBFONTS
 ExpiresByType font/truetype "access plus 1 month"
 ExpiresByType font/opentype "access plus 1 month"
 ExpiresByType application/x-font-woff   "access plus 1 month"
 ExpiresByType image/svg+xml "access plus 1 month"
 ExpiresByType application/vnd.ms-fontobject "access plus 1 month"

 #CSS AND JAVASCRIPT
 ExpiresByType text/css "access plus 1 year"
 ExpiresByType application/javascript "access plus 1 year"
 ExpiresByType text/javascript "access plus 1 year"

     <IfModule mod_headers.c>
         Header append Cache-Control "public"
     </IfModule>
 </IfModule>

In the above example you should use the header append cache-control public.

APACHE DOCS

If you get stuck or have questions regarding the types take a look at: http://httpd.apache.org/docs/2.2/mod/mod_expires.html

5
  • Thanks for your reply its appreciated. I take it that the times represent catch time but, currently I make lots of changes to the js and css of the site will this time frame affect development? Also thanks for the brief explanation in helping me understand caching a bit more. Is this snippet functional? "<FilesMatch "\.(gif|jpg|jpeg|png|swf)$"> ExpiresDefault A604800 Header append Cache-Control "public" </FilesMatch>"
    – kabuto178
    Feb 3, 2013 at 16:10
  • FilesMatch is slightly different, there is many ways to use expires and not all are supported by all hosts. I recommend you testing the mod_expires one as this one has good compatibility though if it doesn't work you can use others. But generally htaccess it is the way forward :P enjoy Feb 3, 2013 at 16:44
  • Thank you, I shall test it out now and htaccess is way more thn i first thought on my way to becoming a webmaster :). Oh I was wondering if the 1 week limit will affect my constantly changing css or js files or when content changes it will re cache?
    – kabuto178
    Feb 3, 2013 at 16:53
  • You shouldn't be needing to change the JS files to often so having a long expire is better, however for CSS I agree that you could be changing this more often so a less expire may be more ideal. Feb 3, 2013 at 18:31
  • Oh man the images were killing my read times, thanks again this definitely made me realize a bit faster loading times
    – kabuto178
    Feb 3, 2013 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.