Web design, SQL, and .NET for the young, up-and-coming developer - Dot Net Yuppie - Blog Archive » Permalinks and mod_rewrite Headaches in Wordpress with IIS
Web design, SQL, and .NET for the young, up-and-coming developer Dot Net Yuppie

I recently switched from Apache/Linux to IIS/Windows in order to develop ASP.NET applications on my shared web host (ixwebhosting.com). Unfortunately, IIS6 doesn’t support WordPress permalinks because it doesn’t have the ability to use mod_rewrite unless you install a third-party ISAPI filter. Because I’m on a shared hosting plan, I don’t have access to install ISAPI filters and my web host won’t install them for me.

There are a few different 404 page-based hacks that allow you to retain your permalinks, even with IIS. The most popular fix, which worked fine for me, was Keyboard Face’s IIS Permalink Fix. I used his fix, and a GET variable fix from Ikailo to make a PHP-based 404 page that redirects all 404 pages back into WordPress, thereby bypassing the need for mod_redirect.

The two fixes above are great, except for the fact that they don’t allow for a real 404 page. In other words, if a visitor types in an invalid URL, they won’t be redirected to a friendly 404 page — they’ll get a blank, white page. This is a common complaint, and to date I haven’t seen a fix for it.

Although I’m not proud of it, I have found a temporary fix until my web host upgrades to IIS7 and/or adopts an ISAPI filter solution for permalink redirections:

<?php

function customError($errno, $errstr, $errfile, $errline)
  {
	echo chr(0);
  }
set_error_handler("customError");

//http://www.keyboardface.com/archives/category/installing-wordpress/
$qs = $_SERVER['QUERY_STRING'];
$_SERVER['REQUEST_URI'] = substr($qs, strpos($qs, ':80')+3);
$_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI'];

//http://www.ikailo.com/94/url-modrewrite-workaround-iis-60/
foreach ( $_GET as $var => $val ) {
	if ( substr($var, 0, 3) == '404') {
		if ( strstr($var, "?") ) {
			$newvar = substr($var, strpos($var, '?') + 1);
			$_GET[$newvar] = $val;
		}
		unset($_GET[$var]);
	}
	break;
}
include('index.php');
?>

As you can see, I added a custom error handler into the script. When the error handler is called, it’ll send a chr(0) code (which for all intents and purposes is a blank, length-less character). Once this code is sent, no header()’s or location-redirects can be sent, meaning that the code won’t die and present a blank page. Instead, it will redirect to your wordpress 404 template page you have installed.

Again, this is not a pretty fix, but it gets the job done.

Update 9/22/2009 – Be advised that this hack will force all your 404 pages to be sent as 200 OK headers, which can hurt your SEO standing with websites. It’s a buggy fix, so hopefully someone more experienced with PHP/WordPress will come up with a better solution soon.

4 Responses to “Permalinks and mod_rewrite Headaches in WordPress with IIS”

  1. John Says:

    Interesting. I’ve been using the custom-404-url workaround for a long time now, never encountered a blank page issue when requesting a page that doesn’t exist.

    But, it does appear to be a real problem for some installations, so there must be ‘something’ that is causing the problem.

    I’d be interested to see what the actual error is that that triggers the blank page. My instinct is telling me it has something to do with the installed theme, or with wp_redirect.

    I wonder if you could try troubleshooting by using this code instead, which should trace it back to an actual file and line number:

    `function customError($errno, $errstr, $errfile, $errline)
    {
    echo “Error Level: $errno\n”;
    echo “Error Message: $errstr\n”;
    echo “Error File: $errfile\n”;
    echo “Error Line: $errline\n”;
    exit(0);
    }
    set_error_handler(“customError”);`

    Feel free to send me the results – I’d love to work on this.

  2. Brian Hurdle Says:

    Thanks for the “fix”. I too host with ixwebhosting.com and I’ve been unable to get this solution to work. One question. Can you tell me what version of Windows and IIS you’re on? While I can create a custom error page through the control panel, it doesn’t seem to work and they have to manually change that for me.

  3. DotNetYuppie Says:

    Are you trying to get it to work on ixwebhosting.com or on your local machine?

    I would assume that all shared hosting plans on ixwebhosting are running IIS6. Initially I had an issue getting the custom 404 page configured with their control panel, and I required assistance initially, but it’s been working since then.

    What does your config panel 404 page look like? Mine is configured: Error code: 404, document type: URL, path: “/wp-404.php” (note that wp-404.php is in the root of dotnetyuppie.com).

    Can you describe the problem you’re running into?

  4. WordPress Permalinks: Getting It Right Using Windows « dotnetof ASP.NET Says:

    [...] Allows for a real 404 page in addition to permalinks [...]

Leave a Reply