【问题标题】:Force browser cache css and js files gzipped with Apache & PHP强制浏览器缓存用 Apache 和 PHP 压缩的 css 和 js 文件
【发布时间】:2012-09-19 15:12:25
【问题描述】:

假设您的服务器不支持 Apache 上的 deflate 和 gzip 模块。在这种情况下,有几种方法可以压缩您的数据。

我使用 Apache 重写模块和 php gzip 扩展来执行此操作。

我创建了一个名为 gzip.php 的文件来获取 $_SERVER['REQUEST_URI']、获取其内容、设置标题、将内容压缩并刷新为一个文件。

我保留了所有文件的扩展名,因此 apache 保留了文件类型。

我在 .htaccess 中添加了这些行:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^((.*)\.(js|css))$ gzip.php [L]

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

我已将这些标题添加到我的文件中:

$src = $_SERVER['REQUEST_URI'];

// seconds, minutes, hours, days
$expires = 60*60*24*14;

ob_start();
ob_implicit_flush(0);

header("Pragma: public");
header("Cache-Control: maxage=".$expires);
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');

// Then do everything you want to do on the page
$path = PUBLIC_DIR . $src;
$info = pathinfo($path);
$ext = strtolower($info['extension']);
include_once 'Entezar/File.php';
$mimeType = Entezar_File::getMimeType($path);
header("Content-type: $mimeType");

if (file_exists($path) && in_array($ext, array('js', 'css'))) {
    $fs = stat($path);
    header("Etag: ".sprintf('"%x-%x-%s"', $fs['ino'],     $fs['size'],base_convert(str_pad($fs['mtime'],16,"0"),10,16)));
    echo file_get_contents($path);
}
unset($path, $src, $info, $ext);

我的问题是,当我使用 apache 重写模块和 php 来压缩内容时,FireFox 根本不会从缓存中加载我的文件(css 或 js)! 谁能帮帮我?!

【问题讨论】:

  • 您是否在浏览器中测试过服务器响应标头?响应标头是否正确?响应正文是否包含您期望的相同内容?
  • 感谢Farzad,现在问题已经解决了!我自己回答的!哦,Zend2 你快乐!

标签: php apache optimization mod-rewrite gzip


【解决方案1】:

挖掘者的发现者! 在做任何工作之前(压缩 gzip.php 文件中的一些文件)你应该检查 $_SERVER 变量中的这两个键(当然你应该在 apache .htaccess 文件或其他地方设置过期和缓存头......):

$etag = '"' .  md5($contents) . '"';
$etag_header = 'Etag: ' . $etag;
header($etag_header);

if (isset($_SERVER['HTTP_IF_NONE_MATCH']) and $_SERVER['HTTP_IF_NONE_MATCH']==$etag) {
    header("HTTP/1.1 304 Not Modified");
    exit();
}

在 apache .htaccess 中添加这些行:

<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"
</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> 

【讨论】:

    猜你喜欢
    • 2012-01-28
    • 2011-09-18
    • 1970-01-01
    • 1970-01-01
    • 2016-04-12
    • 2012-08-03
    • 2011-09-12
    • 2012-02-23
    • 2014-10-18
    相关资源
    最近更新 更多