大多数网站希望压缩它们的内容以节省带宽。然而,当涉及到运行 PHP 的 Apache 服务器时,有两种方法可以实现 - 使用 PHP 或者使用 Apache。那么哪一种方法在你的服务器上更快或更容易呢?
举个例子,在 PHP 中,我会在页面开始时运行以下函数来启用它:
/**
* Gzip compress page output
* Original function came from wordpress.org
*/
function gzip_compression() {
//If no encoding was given - then it must not be able to accept gzip pages
if( empty($_SERVER['HTTP_ACCEPT_ENCODING']) ) { return false; }
//If zlib is not ALREADY compressing the page - and ob_gzhandler is set
if (( ini_get('zlib.output_compression') == 'On'
OR ini_get('zlib.output_compression_level') > 0 )
OR ini_get('output_handler') == 'ob_gzhandler' ) {
return false;
}
//Else if zlib is loaded start the compression.
if ( extension_loaded( 'zlib' ) AND (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE) ) {
ob_start('ob_gzhandler');
}
}
另一种选择是使用Apache的deflate或gzip(两者都非常接近)。要启用它们,您可以将以下内容添加到您的.htaccess文件中:
(此处链接到相关问题) (此处链接到相关问题)(此处链接到相关问题)AddOutputFilterByType DEFLATE text/html text/plain text/xml application/x-httpd-php
由于PHP是一种脚本语言(必须通过PHP加载),我认为Apache方法应该更加稳定和快速。但是在现实世界中假设不太有用。
毕竟,你可能会认为由于Windows拥有巨大的财力支持......呃,我们就不去那里了。