Overview of Gzip Encoding and Caching
Before I discuss the detailed steps, you should know something about Gzip encoding and caching. Gzip encoding speeds up a website by compressing the content from the server to the client browser. This reduced file size is what speeds up loading. See the flowchart below:
Enabling gzip encoding requires you to have full access to your website files and source code. By default, your server and WordPress does not have gzip encoding enabled, so you need to tweak your website to enable it.
On the other hand, caching improves your website's loading performance, particularly if you have a fairly large WordPress website. WordPress creates a dynamic website, which means it relies on both PHP and a MySQL database to serve content.
Refer to the diagram below for the differences between non-cached and cache- enabled WordPress websites:
If caching is enabled in WordPress using a plugin, it caches the WordPress content in advance and converts it to plain, static HTML files. This means there will be no need to execute PHP scripts and communicate to MySQL databases to fetch content.
So if there is a content request, WordPress automatically renders the cached, static HTML files to the client browser, thus improving both the server's and website's loading performance.
Heavy PHP script execution consumes a lot of CPU resources, and in a traffic spike situation (for example, if your website post gets featured in Digg), there is a strong chance your site will go down due to this problem.
With caching enabled, your WordPress website will be protected from this problem.
Enabling Gzip Encoding in WordPress
There are different ways to enable Gzip encoding in a WordPress website. These include using htaccess; using PHP; and using a WordPress plugin.
Not all web hosting servers have enabled the mod gzip directive in Apache. If your web host's servers fall into this category, you cannot use the .htaccess method.
Also, some servers are sensitive to changes in .htaccess configuration. In this case, if you have enabled Gzip encoding via a WordPress plugin, it may not work, and will introduce some problems.
The best and easiest way to enable Gzip encoding in a WordPress website involves using PHP script. To make sure gzip encoding has been implemented correctly, follow the steps below:
Step 1. Go to this tool: http://www.gidnetwork.com/tools/gzip-test.php and try entering your website's home page URL. If you see "NO" for "Web Page compressed?", then gzip encoding is NOT enabled for your website.
Step 2. Download your WordPress index.php file from your server root directory. Open it up using your favorite PHP editor. You should see the lines shown below:
<?php
/**
* Front to the WordPress application. This file doesn't do anything, but loads
* wp-blog-header.php which does and tells WordPress to load the theme.
*
* @package WordPress
*/
/**
* Tells WordPress to load the WordPress theme and output it.
*
* @var bool
*/
define('WP_USE_THEMES', true);
/** Loads the WordPress Environment and Template */
require('./wp-blog-header.php');
?>
Step 3. Back up your original index.php file, and then add this line code just below <?php:
ob_start("ob_gzhandler");
So the final index.php code will be:
<?php
ob_start("ob_gzhandler");
/**
* Front to the WordPress application. This file doesn't do anything, but loads
* wp-blog-header.php which does and tells WordPress to load the theme.
*
* @package WordPress
*/
/**
* Tells WordPress to load the WordPress theme and output it.
*
* @var bool
*/
define('WP_USE_THEMES', true);
/** Loads the WordPress Environment and Template */
require('./wp-blog-header.php');
?>
Step 4. Upload the edited index.php to your website's root directory.
Step 5. Retest your website's home page using this tool: http://www.gidnetwork.com/tools/gzip-test.php to make sure that gzip encoding has now been enabled. The value of "Web page compressed?" should now be "Yes."
Step 6. Try entering other WordPress URLs, for example post URLs, to find out if gzip encoding has been implemented sitewide. It should be enabled sitewide.
IMPORTANT: When upgrading your WordPress, the line you added, ob_start("ob_gzhandler"); will be removed from index.php. Therefore, you should manually add that line again after a WordPress upgrade to enable gzip encoding.
No comments:
Post a Comment