WordPress Tutorial: Using WP-Cache on Windows / IIS

Is your blog starting to bog down? Getting nasty emails from your ISP about overloading the database server? Since most blogs are read far more often than they are updated, caching your pages can result in a real performance improvement.

WordPress has some very basic object caching, but you really need to be able to cache whole pages to see a big benefit. Luckily there is a very good page-caching plugin, WP-Cache.

If you are on a Linux or Unix host, installation is pretty straightforward.

Now, what if you are on a Windows/ IIS host and using ‘date and name based’, almost-pretty permalinks? No sweat. Okay, a little bit of sweat.

The code for WP-Cache makes a few assumptions about the environment it’s running in which don’t work out so well in Windows. My first major step in getting it to work was a great blog post on CPUIdle. Since that blog seems to be down, I’ll quote their steps here:

“1. Download WP-Cache zip file (current version as of writing is 2.0.17) and unzip into wp-content/plugins folder.

2. Copy wp-content/plugins/wp-cache/wp-cache-phase1.php to wp-content/advanced-cache.php (not really sure why this isn’t simplified by the author).

3. Open the standard wp-config.php file and add define(‘WP_CACHE’, true);

4. Now comes the tricky part:

open wp-content/plugins/wp-cache/wp-cache.php in your favourite text editor. Search for the wp_cache_add_pages function and change the function code like this:

add_options_page(‘WP-Cache Manager’, ‘WP-Cache’, 5, ‘wp_cache/wp_cache.php’, ‘wp_cache_manager’);

Reason the original code doesn’ work is that the original __FILE__ resolves to wp_cache\wp_cache.php which some browser eat and convert to wp_cachewp_cache.php- which doesn’t exist.

5. Second problem is that WP-Cache checks for installation step 2) in a windows-incompatible manner. Search for the wp_cache_check_link function. Change the first three lines after the variable declaration in this way:

# if ( basename(@readlink($wp_cache_link)) != basename($wp_cache_file)) {

# @unlink($wp_cache_link);

# if (!@symlink ($wp_cache_file, $wp_cache_link)) {

if (!file_exists($wp_cache_link)) { {

6. Finally, open wp-content/plugins/wp-cache/wp-cache-phase2.php and search for ob_end_clean(); and replace with ob_end_flush();. Without this change the cached page contents are not written back when the page is initially cached. It’s unclear to me if that works under *nix, I assume it couldn’t.

7. That’s it- you’re done. No goto Options/WP-Cache and turn caching on.”

Unfortunately, if you are set up like we are, using the “index.phpâ€? style permalinks, there’s one last step you’re going to have to do. In Windows / IIS, $_SERVER['REQUEST_URI'] is blank. You need to use $_SERVER['SCRIPT_NAME'].$_SERVER['PATH_INFO'] instead. If you don’t, WP-Cache will happily cache your index.php file, but it will also think your /index.php/category/cheese/ page and your /index.php/2006/01/01/I-am-very-interesting/ page are the same as index.php.

In wp-cache-phase1.php (and also advance-cache.php) look for this line:

$key = md5(preg_replace(‘/#.*$/’, ”, $_SERVER['REQUEST_URI']) . wp_cache_get_cookies_values());

and change it to this:

$key = md5(preg_replace(‘/#.*$/’, ”, $_SERVER['SCRIPT_NAME'].$_SERVER['PATH_INFO']) . wp_cache_get_cookies_values());

By the way, one nice thing about step 6 above is that it also fixes a blank-page bug that some people have run into.

Finally, what if you want to use both WP-Cache and gzip? Here’s how.


Written by Jason

Last 3 posts by Jason

  1. If you just read this article, you might have been curious enough to look at the source to see if I was practicing what I preached above. If it doesn’t look like Unsought Input has WP-Cache on, there are two reasons:

    1) I don’t always leave the WP-Cache’s comment on. It’s really only useful when testing.

    2) We’ve had some server issues here and there so I’ve turned it off temporarily. So don’t Slashdot us for a little while, okay?

    Jason
    October 31st, 2006 at 10:19 pm
  2. It’s Nice! I follow your step and got Wp-cache works.

    but just 1 problem: When I enable wp-cache, the search function in wordpress doing sommething wrong,it just return to blog homepage.

    Once I disable wp-cache , the search works fine.

    Is this caused by the code “$_SERVER[’SCRIPT_NAME’].$_SERVER[’PATH_INFO’]“, and how to solve it?

    please help me!

    HighDiy
    November 9th, 2006 at 4:51 pm
  3. Found the solution to make the search page dynamic, and now everything seems to work on IIS:

    Just follow “How do you get cruft free URIs for search results?� :

    http://codex.wordpress.org/FAQ_Advanced_Topics

    And then add “index.php/search/� to the url-strings that wp-cache should ignore.

    snakefoot
    January 4th, 2007 at 8:47 pm
  4. I’m trying to get WP2.1 and WP-Cache 2.1 working together on my site which is hosted on a windows/IIS setup – and it’s not happening. I’ve followed all your directions and managed to get a far as being able to see the cache configuration page under ‘options’ on the dashboard – but when I use the site I end up only seeing the front page all the time. When I try to click into any specific article or follow a “MORE” link to the longer version of a post – it just remains on the front index.php page.

    I’ve disabled the plugin for now and gone back to normal – but I’d love to get it working to boost the performance of my site. Any suggestions greatly welcomed; please email me back. Thanks.

    c0y0te

    Coyote
    February 18th, 2007 at 9:19 am
  5. I’m getting permission errors when I go to options page:

    Warning: fopen(c:\websites\digitalpickles115\thebitbag.com/wp-config.php) [function.fopen]: failed to create stream: Permission denied in c:\websites\digitalpickles115\thebitbag.com\wp-content\plugins\wp-cache\wp-cache.php on line 395

    The folder and the files have full permissions. i don’t understand what’s going on.

    Torrence Davis
    February 20th, 2007 at 12:15 am
  6. Re: Torrence, try adding everyone access to the root of the main wordpress directory, go to options, then remove the permission you just added.

    Re: search form, windows won’t work with the ‘pretty search url’ so instead I added

    ?s=

    in the list of exclusions in the wp-cache options page.

    Ian
    March 4th, 2007 at 12:03 pm
  7. If you are using isapi_rewrite for nice permalinks, and you keep getting the same page from the cache no matter what link you follow, then you may want to try this key instead:

    $key = md5($_SERVER['SERVER_NAME'].preg_replace(‘/#.*$/’, ”, $_SERVER['SCRIPT_NAME'].$_SERVER['QUERY_STRING']).wp_cache_get_cookies_values());

    This worked for me

    Yonatanz
    May 7th, 2007 at 8:34 am
  8. WP-Cache up and running on IIS!…

    I finally got around to converting wp-cache to work with IIS. Thanks to CpuIdle’s blog for the excellent tutorial, and Yonatanz for his helpful post on Unsought Input that put it over the top!
    I’ll list out all the steps together here since Yonatan…

    Fanrastic.com
    June 6th, 2007 at 10:09 pm
  9. [...] With WP-Cache 2.1.2, the following steps worked for me (the original steps can be found here). [...]

    Microbits » WP Cache on Win32 (IIS)
    July 29th, 2009 at 11:52 pm
  10. I followed the instructions , and when I enabled the plugin , when I requested my website it every time opens from me different page , and when I check the chached pages only show two pages , what could be wrong

    jasten
    August 23rd, 2009 at 2:30 pm

Post a Comment

(or leave a trackback to your blog)