首页 文章

使用Apache和PHP强制浏览器缓存css和js文件

提问于
浏览
1

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

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

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

我保留了所有文件的扩展名,因此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]

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

$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);

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

1 回答

  • 4

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

    $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>
    

相关问题