Skip to content

Instantly share code, notes, and snippets.

@frugan-dev
Forked from aarongustafson/.htaccess
Last active August 29, 2015 14:20
Show Gist options
  • Select an option

  • Save frugan-dev/168c5466daa064ca1834 to your computer and use it in GitHub Desktop.

Select an option

Save frugan-dev/168c5466daa064ca1834 to your computer and use it in GitHub Desktop.

Revisions

  1. @aarongustafson aarongustafson created this gist Mar 9, 2011.
    3 changes: 3 additions & 0 deletions .htaccess
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    # compress the code
    php_value auto_prepend_file ./prepend.php
    php_value auto_append_file ./append.php
    53 changes: 53 additions & 0 deletions append.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    <?php

    /* -----------------------------------------------------------------------------
    * Function: tidy_up( $buffer )
    * Purpose: Clean up the markup
    * ---------------------------------------------------------------------------*/
    function tidy_up( $buffer )
    {
    # remove extra paragraphs
    $buffer = preg_replace( '/<p><\/p>/', '', $buffer );
    return $buffer;
    }

    /* -----------------------------------------------------------------------------
    * Function: mobilize( $buffer )
    * Purpose: Compresses HTML for mobile delivery
    * ---------------------------------------------------------------------------*/
    function mobilize( $buffer )
    {
    $chunks = preg_split( '/(<pre.*?\/pre>)/ms', $buffer, -1, PREG_SPLIT_DELIM_CAPTURE );
    $buffer = '';
    foreach ( $chunks as $c )
    {
    if ( strpos( $c, '<pre' ) !== 0 )
    {
    # remove new lines & tabs
    $c = preg_replace( '/[\\n\\r\\t]+/', ' ', $c );
    # remove extra whitespace
    $c = preg_replace( '/\\s{2,}/', ' ', $c );
    # remove inter-tag whitespace
    $c = preg_replace( '/>\\s</', '><', $c );
    # remove CSS & JS comments
    $c = preg_replace( '/\\/\\*.*?\\*\\//i', '', $c );
    }
    $buffer .= $c;
    }
    return $buffer;
    }

    $buffer = ob_get_clean();

    # don't compress JS files this way (some aren't ready for it)
    if ( preg_match( "/\.js/", $_SERVER['REQUEST_URI'] ) === 0 )
    {
    echo mobilize( tidy_up( $buffer ) );
    }
    else
    {
    # you could use YUI compressor here
    echo $buffer;
    }

    ?>
    1 change: 1 addition & 0 deletions prepend.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    <?php ob_start(); ?>