开发者

static images aren't caching with php-generated page

Our website was just converted to being generated by mod_rewrite and php scripts. Images aren't caching in browsers when they seemingly should be.

All images follow format:

<img src="/images/header.png" />

I must avoid the script completely caching because the PHP parser needs to handl开发者_开发百科e each page dynamically on each request; however, the download overhead of the large images is cumbersome on every single page load.

I would ideally provide headers for "Cache-Control: no-cache, must-revalidate" and "Expires: some_date_in_the_past" to force revalidation of the PHP script.

Why isn't the browser caching static images with consistent href values across all pages?


You can tell in your .htacess file what file extensions you want to apply certain headers on so for static content you could provide a far future expires header and for dynamic content you may wish to write cache-control no cache must revalidate.

<IfModule mod_deflate.c>
<FilesMatch "\.(js|css|htm|php)$">
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>

<IfModule mod_expires.c>
    <FilesMatch "\.(gif|jpg|jpeg|png|swf|css|js|html?|xml|txt)$">
        ExpiresActive On
        ExpiresDefault "access plus 10 years"
        ExpiresByType image/x-icon A604800
        Header set Cache-Control "max-age=290304000, public"
    </FilesMatch>
</IfModule>

Like in this example i have added a far future expires header to all different static file matches.

Note the FileMatch tag and the file extensions. The .htacess file is kept in any folder inside your application and it gives that folder these settings.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜