if (mb_eregi ("local root address", $ _SERVER ['DOCUMENT_ROOT'])) {
include ('ready.php');
} function ready ($ dir) {
$ dir = $ _SERVER ['DOCUMENT_ROOT']. $ dir;
$ ext = array ("js", "css");
for ($ i = 0; $ i <count ($ ext); $ i ++) {search ($ dir, $ ext [$ i]);
}}
function search ($ dir, $ ext) {
$ dirH = opendir ($ dir);
while (($ file = readdir ($ dirH))! == false) {
if ($ file! = "." && $ file! = ".." &&! mb_eregi (". gzip", $ file)) {
if (filetype ($ dir. $ file) == "dir") {
search ($ dir. $ file. "/", $ ext);
} else {
if (fnmatch ("*.". $ ext, $ file)) {
if (! mb_eregi ("gzip", $ file)) {
// next line will show all found files
// print $ dir. $ file. "<br>";
$ adr = substr ($ dir. $ file, 0, strrpos ($ dir. $ file, "."));
$ timeF = filemtime ($ dir. $ file);
if (is_file ($ adr. ". gzip.". $ ext)) {
$ timeG = filemtime ($ adr. ". gzip.". $ ext);
}
if ($ timeF> $ timeG) {
// the next line will show the files to be compressed
// print $ dir. $ file. "- GZIP <br>";
// minimized (need yuicompressor and its real address)
exec ("java -jar yuicompressor.jar". $ adr. ".". $ ext. "-o". $ adr. ". gzipY." $ ext);
// compress
if (is_file ($ adr. ". gzipY.". $ ext)) {
shell_exec ("gzip -9 -n -f -c". $ adr. ". gzipY.". $ ext. ">". $ adr. ". gzip.". $ ext);
unlink ($ adr. ". gzipY.". $ ext);
} else {
shell_exec ("gzip -9 -n -f -c". $ adr. ".". $ ext. ">". $ adr. ". gzip.". $ ext);
}
}}}}}}
closedir ($ dirH);
}
// Here we set the address where the files are located
ready ("address"); <link href = "/ css / css.v = <? = filemtime ($ _ SERVER ['DOCUMENT_ROOT']." css / css.css ");?>. css" rel = "stylesheet" type = "text / css ">
<link href = "/ css / css.v = 1263208288.css" rel = "stylesheet" type = "text / css">
RewriteEngine on
ReWriteCond% {HTTP: accept-encoding} gzip
RewriteRule ^ (. * \.) V = [0-9.] + \. (Js | css) $ /$1gzip.$2 [QSA, L]
ReWriteCond% {HTTP: accept-encoding}! Gzip
RewriteRule ^ (. * \.) V = [0-9.] + \. (Js | css) $ / $ 1 $ 2 [QSA, L]
<FilesMatch. * \. Gzip \. (Js | css) $>
Header set Content-Encoding: gzip
Header set Cache-control: private
</ FilesMatch> Source: https://habr.com/ru/post/80848/
All Articles