我使用.htaccess
由http://html5boilerplate.com/提供,它正確壓縮.html,.css,& .js文件,但我無法獲得.php文件進行壓縮,除非我將<?php if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start(); ?>
添加到頁面的頂部。我想要做的最後一件事是將該行添加到一百多頁。如何使用.htaccess或php.ini使用gzip壓縮來優化php文件?
我一直在使用下面的兩個網站,以驗證gzip壓縮:
下面的建議沒有工作之一:
我也試過以下(http://perishablepress.com/fast-effective-php-compression/):
<?php // placed in a gzip_start.php file
ob_start("ob_gzhandler");
?>
<?php // placed in a gzip_stop.php file
ob_flush();
?>
# Placed in the .htaccess filew
# dual file includes for PHP compression
php_value auto_prepend_file /specify/full/path/to/gzip_start.php
php_value auto_append_file /specify/full/path/to/gzip_stop.php
這是我的.htaccess
文件的Gzip已部分:
# ----------------------------------------------------------------------
# Gzip compression
# ----------------------------------------------------------------------
<IfModule mod_deflate.c>
# Force deflate for mangled headers developer.yahoo.com/blogs/ydn/posts/2010/12/pushing-beyond-gzipping/
<IfModule mod_setenvif.c>
<IfModule mod_headers.c>
SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s,?\s(gzip|deflate)?|X{4,13}|~{4,13}|-{4,13})$ HAVE_Accept-Encoding
RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
</IfModule>
</IfModule>
# HTML, TXT, CSS, JavaScript, JSON, XML, HTC:
<IfModule filter_module>
FilterDeclare COMPRESS
FilterProvider COMPRESS DEFLATE resp=Content-Type /text/(html|css|javascript|plain|x(ml|-component))/
FilterProvider COMPRESS DEFLATE resp=Content-Type /application/(javascript|json|xml|x-javascript)/
FilterChain COMPRESS
FilterProtocol COMPRESS change=yes;byteranges=no
</IfModule>
<IfModule !mod_filter.c>
# Legacy versions of Apache
AddOutputFilterByType DEFLATE text/text text/html text/plain text/css application/json
AddOutputFilterByType DEFLATE text/javascript application/javascript application/x-javascript
AddOutputFilterByType DEFLATE text/xml application/xml text/x-component
</IfModule>
# Webfonts and SVG:
<FilesMatch "\.(ttf|otf|eot|svg|php)$" >
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>
如果你在你的項目中有一個'百加pages'那麼也許你需要重新考慮你的設計模式 –
什麼是'內容Type'服務器發送時你訪問php文件? – Gerben
@Gerben優秀的問題。 '的Content-Type:text/html的; charset = UTF-8' – Micah