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 8478129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:39:35+00:00 2026-06-10T18:39:35+00:00

I want the following things to occur: Have the process automated server side. Simply

  • 0

I want the following things to occur:

  1. Have the process automated server side.

  2. Simply be able to reference the LESS file as I would a CSS file in my code.

  3. The user is returned minified CSS instead of the LESS file – cached so the compiler doesn’t need to run unless the LESS file has been updated.

  4. For this to work with any LESS file that is referenced anywhere within my domain.

I spotted Lessphp, but the documentation isn’t very clear, nor does it explain how to dynamically get any LESS file to it. I thought I would post up how I got it all working as I haven’t seen a run through on how to achieve this with PHP.

  • 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-06-10T18:39:36+00:00Added an answer on June 10, 2026 at 6:39 pm

    THIS ASSUMES LESSPHP v0.3.8+ Unsure about earlier versions, but you’ll get the gist of how it works if it doesn’t straight out of the box.

    <link rel="stylesheet" type="text/css" href="styles/main.less" />

    If you were using less.js to compile client side, make sure you change rel="stylesheet/less" to rel="stylesheet"

    1) Grab Lessphp I placed these files in /www/compilers/lessphp/ for the context of this demo

    2) Make a PHP script that we can throw out LESS files at. This will deal with caching, compiling to CSS and returning the CSS as a response. I have placed this file at /www/compilers/ and called it lessphp.php

    Most of this code was on the Lessphp site, except there were errors in it, and I have added the response at the end.

    <?php
    require "lessphp/lessc.inc.php";
    $file = $_GET["file"];
    function autoCompileLess($inputFile, $outputFile) {
        // load the cache
        $cacheFile = $inputFile.".cache";
        if (file_exists($cacheFile)) {
            $cache = unserialize(file_get_contents($cacheFile));
        } else {
            $cache = $inputFile;
        }
        $less = new lessc;
        $less->setFormatter("compressed");
        $newCache = $less->cachedCompile($cache);
        if (!is_array($cache) || $newCache["updated"] > $cache["updated"]) {
            file_put_contents($cacheFile, serialize($newCache));
            file_put_contents($outputFile, $newCache['compiled']);
        }
    }
    autoCompileLess('../' . $file, '../' . $file . '.css');
    header('Content-type: text/css');
    readfile('../' . $file . '.css');
    ?>
    

    This will compile the LESS file (eg, styles/main.less) to a cache file and a CSS file (eg, styles/main.less.css).

    3) Add a mod_rewrite rule so that any LESS files a user requests are redirected to our compiler, giving it its path. This was placed in the root .htaccess file.

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
        RewriteRule ^([^.]*\.less)$ compilers/lessphp.php?file=$1 [R,QSA,L]
    </ifModule>
    

    If you are using WordPress, this rule will need to come after it – even if WordPress is in a sub directory, it seems to overwrite these rules, and LESS compilation will not occur for referenced files which exist below (directory wise) WordPress’s .htaccess rules.

    4) Your LESS code should be relatively referenced in relation to the compilers location. Additionally, Lessphp compiler will fail if there are empty attributes, eg. background-color: ;


    If all is working well, the following should occur:

    1. Directly browse your LESS file http://domain.com/styles/main.less

    2. Be automatically redirected to http://domain.com/compilers/lessphp?file=styles/main.less

    3. Be presented with minified CSS

    4. main.less.css and main.less.cache should now exist in the same directory as your LESS file

    5. The last modified dates shouldn’t change unless you update your LESS file

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

Sidebar

Related Questions

I want to develop an application that would extract the following things from an
I want to apply a different hyper link style to the following two things:
Following code gets executed whenever I want to persist any entity. Things seems to
I want a regex that checks the following things: The string starts with an
I tried this, Its not working. I want to do following things: While clicking
I did the following things: + generate keystore.jks with keytool + exported keystore.cer file
I have a little confusion with the following things: Null DBNull.Value When I use
I want to use Gradle to create following things. I want to create a
i have shell script that require to do following things: 1) shell script need
I'm a starting C/C++ programmer. What I want is the following thing: Start program,

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.