0

i'm trying to understanding if is it possibile to avoid request for some embedded objects, loading them directly from cache without asking to web server if the object is valid or not (i don't wont web server response to me with 304 http status code) Is it possible ? Does the expire header works for this way? How?


From my htaccess.

    # cache images/pdf docs for 10 days
    <FilesMatch "\.(ico|pdf|jpg|jpeg|png|gif|js)$">

      Header set Expires "Mon, 31 Dec 2035 12:00:00 gmt"

    </FilesMatch>

    # cache html/htm/xml/txt diles for 2 days
    <FilesMatch "\.(html|htm|xml|txt|xsl)$">
      Header set Cache-Control "max-age=7200"
    </FilesMatch>

</IfModule>

Similar operation with php header function and also in httpd.conf.

I verify the results in the apache access.log. Every time i refresh the page in the access.log file appear the 304 request. So, i think the browser always make the request.

alesdario
  • 326
  • 1
  • 4
  • 11

1 Answers1

2

Yes, you should use Expires or Cache-Control: max-age headers to prevent browser from requesting objects again. See RFC 2616 for details.

HTTP caching works best when caches can entirely avoid making requests to the origin server. The primary mechanism for avoiding requests is for an origin server to provide an explicit expiration time in the future, indicating that a response MAY be used to satisfy subsequent requests. In other words, a cache can return a fresh response without first contacting the server.

AlexD
  • 8,747
  • 2
  • 29
  • 38
  • Thanks a lot, but i'm not understanding how control browser cache. By **.htaccess** , by php' header function, by httpd.conf. Everything i do seems doesn't works. Have you ever done? How? – alesdario Jun 20 '11 at 20:54
  • @alesdario Better show what you have tried so far and what results you have got. – LazyOne Jun 20 '11 at 23:04
  • @LazyOne: see the response below. – alesdario Jun 21 '11 at 07:31
  • 1
    See http://httpd.apache.org/docs/2.0/mod/mod_expires.html how to set Expires and Cache-Control properly. Also, refreshing page in browser by hitting F5 (or Ctrl-R or reload button) is not proper way to test caching because it forces browser to send If-Modified-Since request. Use FireBug/Chrome network panel to check if server actually sends Expires/Cache-Control headers. – AlexD Jun 21 '11 at 08:24
  • I've read documentation. I try this solution, but it absolutely doesn't works. `ExpiresDefault "access plus 4 weeks" ExpiresByType text/html "access plus 1 month 15 days 2 hours" ExpiresByType image/gif "modification plus 5 hours 3 minutes" ExpiresByType image/jpeg "modification plus 5 hours 3 minutes"` – alesdario Jun 21 '11 at 20:18