Satchelmouth’s Weblog
Repository of solutions for things I encounter in my field of work

Pretty Permalinks for WordPress in IIS6

UPDATE : http://www.ikailo.com/94/url-modrewrite-workaround-iis-60/

// 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);

END UPDATE

Originally From : http://tech.einaregilsson.com/2007/07/30/pretty-wordpress-permalinks-on-iis/

Create a 404 page and put the following 4 lines in it:

<?php
$qs = $_SERVER['QUERY_STRING'];
$_SERVER['REQUEST_URI'] = substr($qs, strpos($qs, ':80')+3);
$_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI'];
include('index.php');
?>

All this does is fix the REQUEST_URI and PATH_INFO variables and then include index.php, so WordPress will do the rest. It’s simple, it doesn’t duplicate functionality already in WordPress and it doesn’t have the overhead of another http request for every page hit. The installation steps are:

  1. Create the file wp-404-handler.php in your base WordPress folder.
  2. Set your sites 404 page to point to the wp-404-handler.php url. Most control panels at web hosts allow you to do this. If you’ve got the option to select between FILE and URL then choose URL.
  3. Go to Options -> Permalinks in your WordPress admin page, and choose an appropriate structure for your links. I chose Custom with this pattern:
    /%year%/%monthnum%/%day%/%postname%/ 
  4. Enjoy!
Advertisements

One Response to “Pretty Permalinks for WordPress in IIS6”

  1. Thank you very much, it works!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: