URL Rewriting with WordPress

Filed under Troubleshooting, WordPress

imageI moved this blog from dasBlog to WordPress back around the middle of 2010. At the time, I had gotten pretty permalinks operational just fine. If you don’t know what pretty permalinks are, they’re essentially alternate URLs for the pages of a blog, that are nice and human readable, like www.vbfengshui.com/contact-me instead of www.vbfengshui.com?page=564.

However, just in the last few days, I noticed they weren’t working anymore.

What happened?

Unfortunately, this is one of those situations, where, as a developer and not a web admin, I don’t do this sort of thing often enough to really remember what I do to get things operational. Ask me about multithreading in MSSQL, or how to make the best use of an anonymous delegate in .net, no problem. But configuring WordPress!?! <g>

The problem was, any time I’d try navigating to a pretty URL (say www.vbfengshui.com/contact-me), I’d end up at a page saying

“the system cannot find the file specified”

Not much help.

Googling that error turned up a number of posts, most of which indicates that either .htaccess didn’t have the proper permissions, or that, under IIS, the URL Rewrite module wasn’t installed.

Unfortunately, I’m running hosted IIS, and my host doesn’t provide access to installing things like URL Rewrite.

So what had happened?

After more Googling, I came across this post on the ikailo.com site. The hint (or the reminder as the case may be) I needed was this comment in their 404-handler.php script:

The name of this file.
// Set this value for the URL in Custom Error Properties of your website in IIS.
// Goto: IIS Manager > Websites > [Site Name] > Properties > Custom Errors >
Aha! My host DOES allow me to edit custom 404 handlers, and checking that, I’d already configured it to /wp-404-handler.php. Hmm. Why wasn’t that working? I loaded up FileZilla and started looking around. Lo and behold, that file was no longer there!
Fortunately, the ikailo.com site provides a great little 404 handler script that works just fine with WP 3.2.1. I’m including it here:
<?php
// This is the default file for the site. Usually index.php
$default = 'index.php';
 
// The name of this file.
// Set this value for the URL in Custom Error Properties of your website in IIS.
// Goto: IIS Manager > Websites > [Site Name] > Properties > Custom Errors >
// 404 & 404;2 & 404;3 > URL (Requires a '/' prefix in IIS).
$thisfile = '404-handler.php';
 
$_SERVER['ORIG_PATH_TRANSLATED'] = str_replace($thisfile, $default, $_SERVER['ORIG_PATH_TRANSLATED']);
$_SERVER['SCRIPT_FILENAME'] = str_replace($thisfile, $default, $_SERVER['SCRIPT_FILENAME']);
$_SERVER['ORIG_PATH_INFO'] = str_replace($thisfile, $default, $_SERVER['ORIG_PATH_INFO']);
$_SERVER['SCRIPT_NAME'] = str_replace($thisfile, $default, $_SERVER['SCRIPT_NAME']);
$_SERVER['PHP_SELF'] = str_replace($thisfile, $default, $_SERVER['PHP_SELF']);
$_SERVER['PATH_INFO'] = false;
 
$qs =& $_SERVER['QUERY_STRING'];
$ru =& $_SERVER['REQUEST_URI'];
$pos = strrpos($qs, '://');
$pos = strpos($qs, '/', $pos + 4);
$_SERVER['URL'] = $ru = substr($qs, $pos);
$qs = trim(stristr($ru, '?'), '?');
 
// Required for WordPress 2.8+
$_SERVER['HTTP_X_ORIGINAL_URL'] = $ru;
 
// Fix GET vars
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($default);
?>

Just follow the directions at the top of the file and you should be good to go with nice URLs, even under IIS without the Rewrite module.

I’m guessing that an upgrade of WP at some point removed the old WP-404-Handler.php file. This one, I’ve saved as just 404-handler.php. No idea whether that’ll help to preserve it through the next upgrade but we’ll see.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*