Google PageSpeed Insights的优化:缓存和压缩

时间:2016-04-05 11:30:04

标签: php .htaccess caching deflate

我有一个自动压缩CSS文件的PHP。 它的名字是minified.css.php

对于所有感兴趣的人来说,这就是它的样子:

<?php
    header('Content-type: text/css');
    ob_start("compress");

    function compress( $minify ) {
        // remove comments
        $minify = preg_replace( "/\/\*.+?\*\//", '', $minify );

        // remove tabs, spaces, newlines, etc.
        $minify = str_replace( array("\rn", "\r", "\n", "\t", '  ', '    ', '    '), '', $minify );

        // brackets
        $minify = str_replace( array(' {', '{ '), '{', $minify );
        $minify = str_replace( array(' }', '} '), '}', $minify );
        $minify = str_replace( array(' (', '( '), '(', $minify );
        $minify = str_replace( array(' )', ') '), ')', $minify );

        // (semi)colons
        $minify = str_replace( array(' ;', '; '), ';', $minify );
        $minify = str_replace( array(' :', ': '), ':', $minify );

        return $minify;
    }

    /* css files for combining */
    include(getcwd().'/style.css');

    ob_end_flush();

但Google希望它可以缩减,并且还会添加到期日期。我的.htaccess中的相关部分如下所示:

<IfModule mod_deflate.c>
    Addtype font/truetype .ttf

    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/x-javascript
    AddOutputFilterByType DEFLATE font/truetype
</IfModule>

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 0 month"

    ExpiresByType text/css "access plus 1 month"

    ExpiresByType application/json "access plus 0 seconds"
    ExpiresByType application/xml "access plus 0 seconds"
    ExpiresByType text/xml "access plus 0 seconds"

    ExpiresByType image/x-icon "access plus 1 week"

    ExpiresByType text/x-component "access plus 1 month"

    ExpiresByType text/html "access plus 0 seconds"

    ExpiresByType application/javascript "access plus 1 month"

    ExpiresByType application/x-web-app-manifest+json "access plus 0 seconds"
    ExpiresByType text/cache-manifest "access plus 0 seconds"

    ExpiresByType audio/ogg "access plus 1 month"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType video/mp4 "access plus 1 month"
    ExpiresByType video/ogg "access plus 1 month"
    ExpiresByType video/webm "access plus 1 month"

    ExpiresByType application/atom+xml "access plus 1 hour"
    ExpiresByType application/rss+xml "access plus 1 hour"

    ExpiresByType application/font-woff "access plus 1 year"
    ExpiresByType application/vnd.ms-fontobject "access plus 1 month"
    ExpiresByType application/x-font-ttf "access plus 1 month"
    ExpiresByType font/opentype "access plus 1 month"
    ExpiresByType image/svg+xml "access plus 1 month"
</IfModule>

为了使minified.css.php可缓存+到期日期,我必须添加哪些行?

编辑:使用minified.css.php的标头启用缓存。

header("Cache-Control: max-age=29030400");
header("Cache-Control: public", false); 

但是,压缩问题仍然存在。

1 个答案:

答案 0 :(得分:2)

这是一个php文件,只需添加php的MIME类型:

<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE  application/x-httpd-php
</IfModule>

<IfModule mod_expires.c>
 ExpiresByType application/x-httpd-php "access plus 1 month"
</IfModule>