PHP - compress static css file using gzip

so I have a css file, style.css. in the same directory I have an images / folder. How can I create a script that compresses style.css, but from a different folder?

Now I have this:

<?php
  if(isset($_GET['css'])) $file = array('url' => htmlspecialchars($_GET['css']), 'type' => 'text/css');
  if(isset($_GET['js'])) $file = array('url' => htmlspecialchars($_GET['js']), 'type' => 'application/javascript');
  if(!isset($file)) exit();

  header('Content-type: '.$file['type']);
  header('Cache-Control: max-age='.(60*60*6).', must-revalidate');  

  // whitespace+comment removal, in case gzip is off
  function compress($buffer){
    global $file;
    $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
    $buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), ' ', $buffer);
    return $buffer;
  }

  if(extension_loaded('zlib'))
    if(substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler");

  else ob_start("compress");

  include($file['url']);    
  ob_end_flush();
?>

and use it something like this:

<style type="text/css">
 @import "http://mysite.com/lib/c.php?css=../style.css";
</style>

everything is fine, except that the path to the images inside the css file no longer works. i think because (images / bg.jpg) becomes (../images/bg.jpg)

Is it possible to fix this without moving my c.php script to the same directory as the stylesheet?

+3
source share
1 answer

- ​​, URL-, .

mod_rewrite:

RewriteRule ^images/style\.css$ lib/c.php?css=../style.css

:

@import "http://mysite.com/images/style.css";

script. , URL?

http://mysite.com/lib/c.php?css=.htpasswd
+3

Source: https://habr.com/ru/post/1766200/


All Articles