Forum Moderators: phranque

Message Too Old, No Replies

Cache-control HTTP header.

cannot make the browser request headers

         

zor_sv

3:08 pm on Feb 9, 2008 (gmt 0)

10+ Year Member



Ok so I'm trying to oblige the browser not to request the images and not even their headers for GIF files.

My current settings ("Cache-Control: no-cache") respond like this:
(compares file modification date with server modification date to see if he has to bring a newer version)

http://www.example.com/template/rtl/images/topbar/Void.gif

GET /template/rtl/images/topbar/Void.gif HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12
Accept: image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://www.example.com/
Cookie: PHPSESSID=5cdkbo2mr7s5r6qo3ndpcg48e7; mwc=0001202569330.406057.5; BIMvisited=yes
If-Modified-Since: Mon, 21 Jan 2008 21:01:45 GMT
If-None-Match: "db876c-30-44441ccb7c040"
Cache-Control: max-age=0

HTTP/1.x 304 Not Modified
Date: Sat, 09 Feb 2008 14:52:45 GMT
Server: Apache/2.2.4
Connection: close
Etag: "db876c-30-44441ccb7c040"

I'm interested in adding a setting to Cache-Control variable or change server header format so I could somehow tell the browser that GIFs are VALID for 24 hours so he doesn't have to request their headers, just serve them directly from localhost.

My attempts:
1) Modify cache-control
<FilesMatch "\\.(png¦ico¦¦jpg¦jpeg¦png¦gif¦js¦css¦swf)$"> # 30 days
Header set Cache-Control "public,max-age=2592000"
</FilesMatch>
- in theory would make these file types to expire in 30 days
==> the browser still requests for headers of these files.

2) add ExpireByType
ExpiresActive On
ExpiresByType image/gif "access plus 360 days"
ExpiresByType image/png "access plus 360 days"
ExpiresByType image/jpeg "access plus 360 days"
ExpiresByType image/jpg "access plus 360 days"

-> theory says the file will expire in 360 days from access time
-> this modification adds to server's reply:
Expires: Tue, 03 Feb 2009 15:04:03 GMT
Cache-Control: max-age=31104000

==> the browser STILL requests for header of each file.

Does anyone have experience with HTTP headers?
For my knowledge there is a special header format that would tell the browser to to request headers but I don't know what is that.

Kind regards,
Dragos Rusu

[edited by: jdMorgan at 6:50 pm (utc) on Feb. 9, 2008]
[edit reason] No URLs or sigs, please. See Terms of Service. [/edit]

jdMorgan

7:20 pm on Feb 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This line has a serious problem -- Two consecutive "\" characters at the beginning, two consecutive "¦" characters between "ico" and "jpg", and two instances of "png" in the pattern:

<FilesMatch "\\.(png¦ico¦¦jpg¦jpeg¦png¦gif¦js¦css¦swf)$">

You can rewrite it correctly and more-efficiently as

<FilesMatch "\.(png¦ico¦jpe?g¦gif¦js¦css¦swf)$">

I suggest using both the Cache-control header and mod_expires, as you have done. But don't forget to completely flush your browser cache before testing any new code.

Also, for a slight performance improvement, put your image types in order, from most-requested to least-requested (check your server stats to confirm).
Example:


ExpiresActive On
ExpiresByType image/gif A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType image/jpg A2592000
ExpiresByType image/png A2592000
ExpiresByType image/x-icon A2592000
ExpiresByType text/css A86400
ExpiresByType text/javascript A86400
ExpiresByType application/x-shockwave-flash A2592000
#
<FilesMatch "\.(gif¦jpe?g¦png¦ico¦css¦js¦swf)$">
Header set Cache-Control "public"
</FilesMatch>

Note that the expiry time for images is set to Access plus 30 days; The chances of your images remaining in any cache for longer than 30 days are essentially zero, so there is no use making the Expires time any longer.

I set the Expires time to one day for CSS and JS MIME-types; The chances that you might need to change your CSS or JS files are much higher than for your image files, and you do not want users to have problems for more than a few hours if they have cached the old CSS and JS files!

Make sure the MIME-types in my ExpiresByType list above agree with those declared by your server!

Replace all broken pipe "¦" characters above with solid pipes before use; Posting on this forum modifies the pipe characters.

Jim

[edited by: jdMorgan at 7:22 pm (utc) on Feb. 9, 2008]

zor_sv

6:01 pm on Feb 10, 2008 (gmt 0)

10+ Year Member



Thank you for your reply.

Would this header configuration make the browser stop requesting for headers of the files?

It's very important to me because I have over 70 header request for every page that I serve from which over 30 are pictures (I'd like to reduce that).

jdMorgan

7:21 pm on Feb 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know what you mean by a header... But whether you mean the <HEAD> section of a file or some <include>d header files, it doesn't really matter; If the page URL and all included items have been previously cached, and if the expiry times for these items have not passed, then no request should be sent to your server at all.

Test it and find out.

Jim

phranque

6:57 am on Feb 11, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld [webmasterworld.com], Dragos!

did you mean http HEAD requests [w3.org]?

if so, jim's post applies the HEAD requests as well as GET requests [w3.org].