Forum Moderators: phranque

Message Too Old, No Replies

cache and no cache with .htaccess

         

helenp

6:49 am on Oct 1, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi, my site is semidynamic, so Im not sure what is cached and what is not.
And as the html/php code itself without the images, .js and css etc
arent big files.
Been trying to read about how to set it up in Apache.

To cache all images, css and .js files etc, and when I change them I will have to rename them, but I dont want to cache any html or php code is it correct to set this in the .htaccess file and with that I dont need to add any no cache or expires code etc in the head section of the pages?

<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
ExpiresActive On
ExpiresDefault "access plus 10 years"
</FilesMatch>

<FilesMatch "\.(php|cgi|pl|htm)$">
ExpiresActive Off
Header set Cache-Control "private, no-cache, no-store, proxy-revalidate, no-transform"
Header set Pragma "no-cache"
</FilesMatch>

Thanks,
Helen

phranque

8:38 am on Oct 1, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



yes, if you can configure apache to properly apply the HTTP Response header to your document then you don't need the meta http-equiv element in the document head.

helenp

8:47 am on Oct 1, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



So then the code I done is correct to add to the .htaccess?

How can I know if apache do this properly?
I only want to cash files, not webpages.

Thanks a lot.

helenp

11:36 am on Oct 1, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Dont know if have anything to do if apache can apply it correctly or not. I have a shared hosting.
Anyway I have this in my .htaccess and it works perfect:
AddType application/x-httpd-php5 .htm .html
RewriteEngine On
RewriteCond %{HTTP_HOST} ^marbellasunrentals\.com
RewriteRule (.*) [marbellasunrentals.com...] [R=301,L]

Having a second thought, didnt remember,
as I my html files are served as php, I suppose my php and html code is not cached..:)

helenp

7:12 pm on Oct 1, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Just dont understand how to set it up,
dont want to put an expires code to each page (have nearly 700 pages) and have to change the date.

I installed firebug with pagespeed, and as far as I can see the previous code did not work, and this did not work either:
ExpiresActive On
ExpiresByType image/gif "access plus 10 years"
ExpiresByType image/jpg "access plus 10 years"
ExpiresByType text/js "access plus 10 years"
ExpiresByType text/css "access plus 10 years"
ExpiresByType image/png "access plus 10 years"

as pagespeed said the caching is very short on both, suppose it is to trust.

However found this:
<FilesMatch "\.(jpg|jpeg|png|gif|js|css)$">
Header set Expires "Thu, 1 Oct 2012 20:00:00 GMT"
</FilesMatch>
And as per pagespeed it look like it works, if it is to trust, only the external .js files get error the cache time is short (as google analytics, 2hours).
However with this code I have to remember to change the date and sort of dont like that...
Dont know how to set expires and cache as I dont have these headers in my metas.
Please, need some guiding.

phranque

12:00 am on Oct 2, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



perhaps the mod_expires module hasn't been loaded.

there should be an AddModule or LoadModule directive for that module in your httpd.conf file.

helenp

5:57 am on Oct 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I am on a shared hosting so I doubt they let me touch it.
Anyway, dont know if this is correct, but I added this to .htaccess and as per pagespeed it also worked:
<FilesMatch "\.(jpg|jpeg|png|gif|js|css)$">
Header set Expires "access plus 1 year"
</FilesMatch>
Thanks

tangor

7:27 am on Oct 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Cache where? Bear with me kiddies, I might be missing something. Personally, I don't allow cache ANYWHERE (no-cache, noarchive) because I want all to visit the original source EVERY TIME, including SEs... Am I doing something wrong?

helenp

8:06 am on Oct 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Tangor,
I think you miss something at least if your site is not a small site that loads quickly.

Thats why I only want to cache images, css and .js files.
I have a js file that weight 144 kb, and several smaller, images between 50-100 kb, and that slows down.
So they should be cached.
You should cache everything you can.
So if I update an js file or an css file I rename instead of owerwriting it so then the user download a new version and always get the fresh content.
I dont want to cache the html or php content (it is small anyway)

So what I want is a code if possible in htaccess saying cache .js, .jpeg, .css etc, and another saying dont cache html and php.

phranque

9:38 am on Oct 2, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



tangor, these HTTP Response headers control cacheing by the browser or proxy cacheing which is a completely different concept than the "Cache" link in the search results.
the cache control headers are used for performance reason, both to improve page load speed and reduce server demand.

Helen, read all of jdMorgan's posts in this thread.
Cache control in .htaccess:
http://www.webmasterworld.com/apache/4108533.htm [webmasterworld.com]

helenp

10:46 am on Oct 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks phranque, was a good link say everything I want, and will work I suppose, as header set seems to work,
except, that google recomend expires, jdmorgan says he doesnt as you have to keep changing dates, same as I thought was awful, so I tried this, and it looks like it works...
<FilesMatch "\.(jpg|jpeg|png|gif|js|css)$">
Header set Expires "access plus 1 year"
</FilesMatch>
Just curios about what he says about that, already read he recomended not more than 1 month, but that can be changed.
Thanks a lot

phranque

1:36 pm on Oct 2, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



have you checked your response headers to see if they are valid?
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.21:
The format is an absolute date and time as defined by HTTP-date in section 3.3.1; it MUST be in RFC 1123 date format

i don't think mod_headers has any intelligence to calculate that date for you and the string "access plus 1 year" is meaningless to the user agent.

google recomend expires, jdmorgan says he doesnt as you have to keep changing dates

if you read the end of jdMorgan's last post in that thread, you will see that he clarified his position if you use the relative "access plus 1 year", noting that only works with mod_expires directives rather than mod_headers directives as you are attempting.

helenp

3:13 pm on Oct 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



pranque,
Thanks,
dont know how to check if its valid, only know it was valid for firebug/pagespeed, anyway I have jsMorgands code working (safer), however cant find a list with the numbers for the dates to use.

I did see the end of his post afterwards,
however I found that datetype searching for expiresbytype as it did not work for me, and that way of saying dates is on apaches website(however not for expires but for epiresbytype and default): [httpd.apache.org...]
I also saw it in yahoo developers site:
[developer.yahoo.com...] however is on expiresdefault and not on expires

g1smd

4:37 pm on Oct 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Caching beyond a few hours has little benefit.

Caching beyond a few days has no benefit at all.

helenp

4:49 pm on Oct 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



g1smd
I cant agree with you,
I see my visitors check a property, check another, then check the first again, some days later they check again until they make a decision, and some check even months later just to see whats in there holidayhome etc. and the same .js file are on all pages.
So I think its very important, and you save bandwith that way.

g1smd

5:33 pm on Oct 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You will save no bandwidth at all. Their browser will have flushed the files from cache within a matter of a few days to a few weeks at most.

Additionally, even if they did cache images for months, imagine the time you upload the wrong image by accident. After you correct it, returning visitors will not see the new version because you expressly told them not to check for a year.

helenp

5:56 pm on Oct 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I will not upload wrong images, I always change name on all files I update, as my visitors do come back day after day and they wont clear there cache, Or even me and the staff, the owners, copying the urls, there are many persons returning to same page.
Nobody meant to cache for years, it just a way to say it does not expire.
And all browswers are diferent, as I can see they does not store per date but size of cached files, so it can vary from browser to browser and from person to person.