使用Apache和PHP强制浏览器缓存css和js文件(Force browser cache css and js files gzipped with Apache & PHP)

编程入门 行业动态 更新时间:2024-10-26 21:35:17
使用Apache和PHP强制浏览器缓存css和js文件(Force browser cache css and js files gzipped with Apache & PHP)

想象一下,您的服务器不支持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);

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

Imagine that your server does not support deflate and gzip module on Apache. In this case there are several ways to compress your data.

I use the Apache rewrite module and php gzip extension to do this.

I created one file named gzip.php to get $_SERVER['REQUEST_URI'], get its content, set headers, compress and flush content as a file.

I kept the extension of all files so apache preserves the file types.

I added these lines to .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]

I've added these headers to my files:

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

My problem is that when I use apache rewrite module along php with to compress contents, FireFox does not load my files (css or js) from the cache at all! Can anybody help me?!

最满意答案

Digger的Finder! 在做任何工作(压缩gzip.php文件中的一些文件)之前,你应该检查$ _SERVER变量中的这两个键(当然你应该在某个地方设置expiration和cache headers,比如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>

Digger's Finder! Before doing any work(compress some files in gzip.php file) you should check these two keys in $_SERVER variable(Of course you should set expiration and cache headers in somewhere such as apache .htaccess file or other place... ):

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

In apache .htaccess add these lines:

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

更多推荐

本文发布于:2023-07-31 10:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1341936.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓存   浏览器   文件   Apache   css

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!