什么是expire头以及如何在ASP.NET和PHP中实现它们?

Pra*_*ant 0 php asp.net performance yslow http-headers

今天我用YSlow检查了我网站的性能统计数据.我收到了警告(或可能是错误),如下所示

Add Expires headers
There are 15 static components without a far-future expiration date.

    * (no expires) http://www.example.com/video/css/global.css
    * (no expires) http://www.example.com/video/js/global.js
    * (no expires) http://www.example.com/video/images/main-bg.png
Run Code Online (Sandbox Code Playgroud)

这意味着什么以及如何在PHP和ASP.NET中实现这一点.我在共享托管服务器上,所以请告诉我一些使用代码执行此操作的方法,因为我无法在服务器端进行任何修改.

如果我将使标题过期,那么有可能如果我在CSS中进行更改,那么用户将无法立即获取它们,因为css和其他文件被缓存一段时间限制(1个月,一周).是吗?

使用expire头是否有任何缺点?

Phi*_*ber 5

PHP

$time = time() + 3 * 24 * 60 * 60; // 3 days
header('expires: ' . gmdate('D, d M Y H:i:s \G\M\T', $time);
Run Code Online (Sandbox Code Playgroud)

但我会向你推荐Apache模块mod_expires[1].然后你可以把这样的东西放在.htaccess文件中

<FilesMatch "\.(jpg|gif|png|css|js)$">
    ExpiresActive on
    ExpiresDefault "access plus 3 days"
</FilesMatch>
Run Code Online (Sandbox Code Playgroud)

这将匹配所有图像,CSS和JavaScript文件,并设置Expires标题3天

[1] http://httpd.apache.org/docs/2.0/mod/mod_expires.html