WordPress SEO Optimization: Enable Gzip Encoding and Caching

Google has begun emphasizing the importance of how fast your website loads by including site loading speed as a ranking factor in their search engine. If your website loads slowly, you should take some steps to improve its loading time. This tutorial can help you speed up the loading time of your WordPress website by focusing on Gzip encoding and caching, the two most important factors for improving website loading time.

You can read the Google blog entry in which the company talks about using site speed as a web search ranking factor here.

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.

One of the best caching plugins in WordPress is “Super Cache.” 

To enable the WP Super Cache plugin correctly, follow the steps below:

Step 1. Download your wp-config.php and make sure that this line is deleted (if you cannot see this line, then just proceed to step 2):

define(‘WP_CACHE’, true);

Step 2. Go to the /wp-content directory of your WordPress website, and make sure you’ve deleted these files, if you can see them:

wp-cache-config.php
advanced-cache.php

If you can’t see these files in the /wp-content directory, just proceed to next step.

Step 3. Download this plugin http://wordpress.org/extend/plugins/wp-super-cache/ , extract the folder and then upload the “wp-super-cache” directory to your plugins directory.

Step 4. Log in to your WordPress website as administrator.

Step 5. Activate the wp super cache plugin.

Step 6. Go to Settings ==> Wp Super Cache

Step 7. Select this radio button: “On WP Cache and Super Cache enabled.”

Step 8. Check “Cache rebuild…”

Step 9. Check “Mobile device support.”

Step 10. Click the “Update Status Button.”

Step 11. Check “Test Cache.” There should be no errors reported. If none, proceed to the next step.

Step 12. Check “Preload mode…”

Step 13. Click the “Update Settings” button.

Step 14. Click the “Preload Cache now” button.

Step 15. Scroll down to the Mod Rewrite Rules section. Make sure this is updated; if you are in doubt, you can always click “Update Mod_Rewrite Rules” so that those lines will be added to your WordPress .htaccess. Details about this additional .htaccess syntax can be found here: http://wordpress.org/extend/plugins/wp-super-cache/installation/

That’s it. You do not need to enable compression since you already implemented it in the previous section. Just leave the items below at its default settings:

  • Cache contents
  • Super Cache compression (should be set to Disabled).
  • Expiry Time and garbage collection.
  • Accepted Filenames and Rejected URIs.
  • Rejected User Agents
  • Debug Settings
  • Lock down
  • Directory Cached Files
  • Fix Configuration
  • Cache plugins

The installation and configuration of the WordPress Super Cache plugin is complete. To confirm that you have successfully installed the Super Cache plugin, go to your home page and then view the HTML source code.

Go to the last portion of the HTML code. You should see a comment like this:

The most important point is that you should see the “Cached page generated by WP- Super-Cache.”

Important things you should know about enabling cache

Now that there is virtually no dependence on PHP script to generate and display content to your visitors if caching is enabled, you need to make some adjustments, particularly if you are updating the website with the following common tasks:

  1. Adding new posts/pages.
  2. Updating old posts/pages.
  3. Deleting posts/pages.
  4. Adding new plugins.
  5. Other activities that require obvious PHP script execution in your WordPress website.

You might notice that even if you have new posts or update something on your WordPress website, your server won’t display the changes you have made. This is because it is still displaying the cached content (which is old and needs to be updated).

To update and clear cache, follow the procedure below:

Step 1. Log in as WordPress administrator.

Step 2. Go to Settings ==> WP Super Cache.

Step 3. Under “Cache Contents,” click the “Delete Cache” button.

Step 4. Click the “Delete Expired” button.

Step 5. Click the “Regenerate cache stats” link.

Step 6. Finally, under “Preload Cache,” click the “Preload Cache Now” button.

Try to clear your browser cache and then revisit your website. The changes you have made (e.g adding new posts) should now be reflected on your WordPress website.

Google+ Comments

Google+ Comments