开发者

PHP cache control - possible to avoid ANY connection to the same url from browser?

Is it possible to avoid any connections from the browser at all if first response headers were set properly?

/* Caching control */
$age = 3600*24*30; // 30 days
header('Expires: ' . gmdate('D, d M Y H:i:s ', time() + $age) . 'GMT');
开发者_如何学Python

I have tried to setup this, but the request is reaching the server anyway.

I have found this:

The Expires header has the advantage that it's easy to implement; in most cases, however, unless you're a highly organized person, you won't know exactly when a given page on your site will be updated. Since the browser will only contact the server after the page has expired, there's no way to tell browsers that the page they've cached is out of date. In addition, you also lose some knowledge of the traffic visiting your web site, since the browser will not make contact with the server when it requests a page that's been cached.

The question is, why does Expires header I mentioned above is not working?

** Request Headers **

GET /ru/templates/bannerpartial HTTP/1.1
Host: 192.168.1.3
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.2.12) Gecko/20101026 AlexaToolbar/alxf-1.54 Firefox/3.6.12 FirePHP/0.4
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Cookie: bla-bla-bla
Cache-Control: max-age=0

** Response headers **

HTTP/1.1 200 OK
Date: Tue, 09 Nov 2010 16:48:26 GMT
Server: Apache/2.2.14 (Unix) DAV/2 mod_ssl/2.2.14 OpenSSL/0.9.8l PHP/5.3.1 mod_apreq2-20090110/2.7.1 mod_perl/2.0.4 Perl/v5.10.1
X-Powered-By: PHP/5.3.1
Set-Cookie: ZDEDebuggerPresent=php,phtml,php3; path=/
Expires: Thu, 09 Dec 2010 16:48:26 GMT
Cache-Control: must-revalidate
Content-Encoding: gzip
Vary: Accept-Encoding
Content-Length: 118
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html


It is simpler to use .htaccess file with these settings:

<ifModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 1 seconds"
    ExpiresByType text/html "access plus 10 seconds"
    ExpiresByType image/gif "access plus 31536000 seconds"
    ExpiresByType image/jpeg "access plus 31536000 seconds"
    ExpiresByType image/png "access plus 31536000 seconds"
    ExpiresByType image/ico "access plus 86400 seconds"
    ExpiresByType image/x-icon "access plus 86400 seconds"
    ExpiresByType text/css "access plus 31536000 seconds"
    ExpiresByType text/javascript "access plus 31536000 seconds"
    ExpiresByType application/javascript "access plus 31536000 seconds"
    ExpiresByType application/x-javascript "access plus 31536000 seconds"
    ExpiresByType application/x-shockwave-flash "access plus 31536000 seconds"
<FilesMatch "\.(flv|gif|jpg|jpeg|png|ico)$">
   Header set Cache-Control "max-age=31536000"
</FilesMatch>

AddType image/vnd.microsoft.icon .ico
ExpiresByType image/vnd.microsoft.icon "access plus 1 months"

</ifModule>

If it doesn't work, uncomment the below line in Apache's httpd.conf file (most of the hosted servers enable this module by default):

LoadModule expires_module modules/mod_expires.so

Expires header works only if you click on a link or select the browser's URL bar and press the Enter key. If you press the reload button (or press F5), the browser ignores the expires header and reloads the contents.


Try cache-control: max-age. See this guide for details: http://www.mnot.net/cache_docs/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜