1

サーバーがApacheのdeflateおよびgzipモジュールをサポートしていないと想像してください。この場合、データを圧縮する方法はいくつかあります。

これを行うには、Apacheリライトモジュールとphpgzip拡張機能を使用します。

$ _SERVER ['REQUEST_URI']を取得し、そのコンテンツを取得し、ヘッダーを設定し、コンテンツをファイルとして圧縮およびフラッシュするために、gzip.phpという名前のファイルを1つ作成しました。

すべてのファイルの拡張子を保持したので、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);

私の問題は、phpに沿ってapache rewriteモジュールを使用してコンテンツを圧縮すると、FireFoxがファイル(cssまたはjs)をキャッシュからまったくロードしないことです。誰か助けてもらえますか?

4

1 に答える 1

4

ディガーズファインダー!作業を行う前に(gzip.phpファイルでいくつかのファイルを圧縮する)、$ _ SERVER変数でこれらの2つのキーを確認する必要があります(もちろん、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-09-22T08:08:04.623 に答える