I have problems with caching ...
Im using this php file with url rewriting to compress and cache css and js
I got the impression that if I changed / updated one of my files, the browser will work the updated file. But that doesn’t do it if I didn’t clear the cache or refresh the page.
Is my coding wrong? Or should the browser not receive updated content before the cache expires?
<?php $file = $_SERVER['DOCUMENT_ROOT'].'/'.$_GET['file']; $last_modified_time = filemtime($file); $etag = md5_file($file); $expires = 60*60*24*7; if(file_exists($file)) { if($_SERVER['HTTP_IF_NONE_MATCH'] != $etag) { header("Pragma: public"); header("Cache-Control: maxage=$expires, must-revalidate"); header('Expires: ' . gmdate('D, d MYH:i:s', time()+$expires) . ' GMT'); header("Last-Modified: ".gmdate("D, d MYH:i:s", $last_modified_time)." GMT"); header("Etag: \"{$etag}\""); if($_GET['type'] == 'js') header('Content-type: application/javascript'); if($_GET['type'] == 'css') header('Content-type: text/css'); if($_GET['type'] == 'ico') header('Content-type: image/x-icon'); ob_start("ob_gzhandler"); include($file); } else { header('HTTP/1.0 304 Not Modified'); } } else { header("HTTP/1.0 404 Not Found"); } ?>
rewrite the rules
RewriteRule ^(.*).js$ /compress.php?file=$1.js&type=js [L,QSA] RewriteRule ^(.*).css$ /compress.php?file=$1.css&type=css [L,QSA] RewriteRule ^(.*).ico$ /compress.php?file=$1.ico&type=ico [L,QSA]
---------
EDIT: Maybe I should do it differently? what do large companies do for caching and how to get browsers to get updated content before the cache expires?
EDIT 2: Thank you guy for the help. Im working with cache in 1 hour
source share