Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6104481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:50:49+00:00 2026-05-23T13:50:49+00:00

So I have been using Minify to compress my JS and CSS which had

  • 0

So I have been using Minify to compress my JS and CSS which had all been going nicely until I needed to compress some dynamic php stylesheets.

I tried to use htaccess to fool it into thinking it was a css file, but I then realised it uses absolute file paths which would not be effected by mod_rewrite

Anyways whenever I point it at a php file, it always returns ‘400 Bad Request’. Any idea on how to solve this other than writing my own compression script?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:50:50+00:00Added an answer on May 23, 2026 at 1:50 pm

    I find the best way to deal with minifying and compressing stylesheets is to do it yourself. Check out this code:

    <?php
    header("Content-Type: text/css");
    header("Last-Modified: ".gmdate('D, d M Y H:i:s', filemtime($_SERVER['DOCUMENT_ROOT'].$_SERVER['PHP_SELF']))." GMT");
    header("Expires: ".gmdate('D, d M Y H:i:s', (filemtime($_SERVER['DOCUMENT_ROOT'].$_SERVER['PHP_SELF']) + 691200))." GMT");
    
    //This GZIPs the CSS for transmission to the user
    //making file size smaller and transfer rate quicker
    ob_start("ob_gzhandler");
    ob_start("compress");
    
    //Function for compressing the CSS as tightly as possible
    function compress($buffer) {
        //Remove CSS comments
        $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
        //Remove tabs, spaces, newlines, etc.
        $buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $buffer);
        return $buffer;
    }
    
    //Main style
    require_once($_SERVER['DOCUMENT_ROOT']."/templates/style/style.css");
    
    //Other style
    if($php_variable) {
        require_once($_SERVER['DOCUMENT_ROOT']."/templates/style/other.css");
    }
    
    ob_end_flush();
    ob_end_flush();
    ?>
    

    There’s a lot of things going on here, so let me explain.

    Headers

    • Setting the file type as being CSS.
    • Setting the Last-Modified and Expires header for cache control (Setting at slightly over a week, you can change this).

    Minify and GZIP

    • Using ob_start, we tell it to GZIP this file as well as run a custom function compress.
    • compress removes all CSS comments and white space when sending to to the browser. This means you can keep all your comments and spacing the way you like it in your source files for easier editing, but only send the bare minimum to the browser.

    Style

    • Using require to import the stylesheets. Do this for as many stylesheets as you’d like; They’ll all get delivered in a single HTTP request to the user.

    Using Your New File

    You’ll call on the file just as you would a normal CSS file.

    <style type="text/css" src="/path/to/css-script.php"></style>
    

    Conclusion

    Using this method, you’re doing several awesome things.

    • Provides the correct headers for cache’ing, making return visits to your site quick.
    • Minifys all the contents to contain no extra white space or comments.
    • Compresses the file using GZIP for smaller file size.
    • Includes all your stylesheets in one HTTP request, great for optimization.
    • Avoids using @import statements…which in itself is awesome.
    • Allows you to keep multiple stylesheets and use PHP logic to include or not include them.
    • Allows you to keep your source stylesheets spaced and commented as you like with no consequence to the visitors.

    You can use this same method for JavaScript as well, though the compress function above is strictly for CSS so I would omit it.

    Use this technique in combination with this cache control technique, and you’ve built yourself an awesome CSS/JS handler: How to force browser to reload cached CSS/JS files?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been using C# for a while now, and going back to C++
Have been using a simple CSS only tooltip. Working Example css: .tip { position:relative;
We have been using CruiseControl for quite a while with NUnit and NAnt. For
I have been using PHP and JavaScript for building my dad's website. He wants
I have been using Eclipse as an IDE for a short amount of time
I have been using Castle MonoRail for the last two years, but in a
We have been using Scrum for around 9 months and it has largely been
I have been using ASP.NET for years, but I can never remember when using
I have been using Ruby for a while now and I find, for bigger
I have been using IoC for a little while now and I am curious

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.