简体   繁体   中英

Minification of dynamic PHP pages via htaccess

I have a site with the files all being php files, on the server we also have htaccess doing rewrites for friendly urls, ie /about

We already have minification by PHP for JS and CSS, however due to the fact the HTML is on the actual page is there a way of minifying the html after the php has added in the dynamic bits of the page but before the server sends it back to the client?

I have tried implementing minify, however the callback function breaks the page with no errors.

Use this before <doctype html> tag or config file

function sanitize_output($buffer)
{
    $search = array(
        '/\>[^\S ]+/s',  // strip whitespaces after tags, except space
        '/[^\S ]+\</s',  // strip whitespaces before tags, except space
        '/(\s)+/s'       // shorten multiple whitespace sequences
        );
    $replace = array(
        '>',
        '<',
        '\\1'
        );
    $buffer = preg_replace($search, $replace, $buffer);
    return $buffer;
}
ob_start("sanitize_output");

If we're talking about dynamically minifying everything on the fly, I would council you to be cautious about seeing this as an optimisation.

Yes, there may be a significant bandwidth saving to be had. And yes, it is possible that your users may get a performance boost from the smaller downloads.

But, if you're minifying your files on the fly using PHP (which is what it sounds like you're doing), it is also possible that you may actually be slowing things down for the end user.

Minification is best done on static files. Minify them once, save them, and serve the minified file to the browser without any further processing when the file is requested. Doing it in PHP every time a file requested can be a significant performance overhead which could easily counteract the gains from the smaller download size.

Minifying HTML is usually less effective than Javascript. The best way to 'minify' your PHP-generated HTML is to cut out the unnecessary white space from your HTML template files. This is usually a pretty easy task, and doesn't need a dedicated minifier tool -- simplest way to do it is just to remove all indentation from the file. White space is relevant in HTML, so you can't just get rid of it all, but losing the indentation should deal with most of it, while conveniently still leaving the file reasonable readable.

So my advice is to minify your files statically if possible. But if you must do it on the fly, don't use PHP for the job. Use a dedicated server plug in, such as Google's mod_pagespeed . This plugin will minify the files for you when they're requested (at much higher speed than PHP could do it), and also takes care of caching the minified version, to minimise the performance impact.

Hope that helps.

You could look into output buffering in PHP, which would allow you the final minification step. If your aims is to minimise for download there's also gZip encoding to look into.

This may help: how to minify php page html output

There is one script I came across recently that used to minify your HTML source code on the fly, script called as Dynamic Website Compressor

it's a PHP based script, but can be used with .htaccess

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM