元缓存标记和htaccess缓存之间的区别?

时间:2014-11-20 06:47:14

标签: html css html5 .htaccess caching

我目前在我的.htaccess文件中有资源缓存:

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access 1 year"
    ExpiresByType image/jpeg "access 1 year"
    ExpiresByType image/gif "access 1 year"
    ExpiresByType image/png "access 1 year"
    ExpiresByType text/css "access 1 month"
    ExpiresByType text/html "access 1 month"
    ExpiresByType application/pdf "access 1 month"
    ExpiresByType text/x-javascript "access 1 month"
    ExpiresByType application/x-shockwave-flash "access 1 month"
    ExpiresByType image/x-icon "access 1 year"
    ExpiresDefault "access 1 month"
</IfModule>

我是否还需要在我的网页上使用Cache-Control元标记才能应用这些规则?即。

<meta http-equiv="Cache-control" content="public">

1 个答案:

答案 0 :(得分:1)

两者都设置了不同的标题和值,浏览器中的结果将与您使用的方法相同。主要区别在于每个文件可以应用的文件类型:

*a <meta> tag can only appear inside an HTML document; consequently, you cannot use it to control cache validity on non-HTML files (such as images), and it's possible that something would download the file without processing the HTML and spotting the tag.*

*the Apache Header directive (not part of mod_rewrite) can be applied to any file that Apache is serving, whether it is dynamically generated or not. This can include images, static HTML, CSS, and JS, plus whatever content you generate with PHP.*

Apache指令的唯一缺点是,根据您的设置和Apache配置规则的经验,可能难以为不同的数据位维护更复杂的规则。

您可能想尝试使用PHP,如下所示:

the PHP header() function can affect any file that you are serving dynamically with PHP. This could include, for instance, plain text or XML files, but might not include an HTML file you'd uploaded fully formed to your server. You could pass static files, including images, through PHP, but this would be an inefficient way of serving them.