compress/optimize css, reduce apache requests

Started on the old forum in December 2010 · view the original in the archive · old links redirect here

No replies · 808 views · started 15 years ago · 1 min read

GumSlone Administrator Principal · 1,690 posts #1

here is a small piece of code which will make you site load faster if you use multiple css files on it. put it intoo the directory with your css files.

link to it will look like this:

<link rel="stylesheet" href="./styles/style.css.php" title="stylesheet" type="text/css" />

i'm using this code at my socialengine site.

<?php
$offset = 60 * 60 * 24 * 30; // Cache for a month
header("Content-type: text/css");
header ("Cache-Control: max-age=" . $offset . ", must-revalidate");
header ("Expires: " . gmdate ("D, d M Y H:i:s", time() + $offset) . " GMT");

  ob_start("compress");
  function compress($buffer) {
    /* remove comments */
    $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
    /* remove tabs, spaces, newlines, etc. */
    $buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $buffer);
    /* remove last semicolon */
    $buffer = str_replace(array(";}", "; }"), '}', $buffer);
    return $buffer;
  }

  /* your css files */
  include('styles_global.css');
  include('styles.css');
  include('styles_album.css');
  include('styles_music.css');
  include('styles_blog.css');
  include('styles_video.css');
  include('styles_group.css');
  include('styles_apps.css');
  include('styles_newsfeed_advanced.css');
  include('styles_360player.css');
  include('gum_styles.css');
  include('shadetabs.css');
    ob_end_flush();
?>

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

Write your reply now; next you create a free account (or sign in) and it is posted right away. Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close