在PHP中添加Expire Headers无法使其正常工作

Waz*_*azy 16 php performance .htaccess yslow

我安装了Yslow附加组件

替代文字

当我在Yslow中检查我的应用程序时,我得到了Add Expires标题,我不知道

替代文字

我搜索了SO中的相关问题,Google也发现这种方法合适

<?
    header("Expires:".gmdate('D, d M Y H:i:s \G\M\T', time() + 3600));
    header("Cache-Control: no-cache");
    header("Pragma: no-cache");
    ob_start();
    session_cache_limiter('public');
    session_start();
?>
<html>
Run Code Online (Sandbox Code Playgroud)

但它仍然表明我一样

因为我是新手,我对.htaccess了解不多

请帮助我提高应用程序性能

提前致谢

Wazzy

Jak*_*e N 13

这只会为你的页面内容设置它而不是像图像和css文件这样的东西,我在你的截图上注意到它说42个文件,大概这些是你的图像,css,js等.

在你的.htaccess文件中试试这个,注意这只有在Apache中启用mod_expiresmod_headers启用时才有效:

<ifModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault "access plus 1 seconds"
  ExpiresByType text/html "access plus 1 seconds"
  ExpiresByType image/gif "access plus 2592000 seconds"
  ExpiresByType image/jpeg "access plus 2592000 seconds"
  ExpiresByType image/png "access plus 2592000 seconds"
  ExpiresByType text/css "access plus 604800 seconds"
  ExpiresByType text/javascript "access plus 216000 seconds"
  ExpiresByType application/x-javascript "access plus 216000 seconds"
  ExpiresByType application/javascript "access plus 216000 seconds"  
</ifModule>

<ifModule mod_headers.c>
  <filesMatch "\\.(ico|pdf|flv|jpg|jpeg|png|gif|swf)$">
    Header set Cache-Control "max-age=2592000, public"
  </filesMatch>
  <filesMatch "\\.(css)$">
    Header set Cache-Control "max-age=604800, public"
  </filesMatch>
  <filesMatch "\\.(js)$">
    Header set Cache-Control "max-age=216000, private"
  </filesMatch>
  <filesMatch "\\.(xml|txt)$">
    Header set Cache-Control "max-age=216000, public, must-revalidate"
  </filesMatch>
  <filesMatch "\\.(html|htm|php)$">
    Header set Cache-Control "max-age=1, private, must-revalidate"
  </filesMatch>
</ifModule>
Run Code Online (Sandbox Code Playgroud)