If you’re like me and have a web hosting company serve up your WordPress blog, you might not have a lot of control over IIS. This can be a real pain when trying to setup “pretty permalinks“. I didn’t like or want the default http://blackmarketserver.com/index.php?[name_of_article] link structure, so I set out to find a way to get my permalinks working correctly. It took me a little Google searching to find a solution that worked for me, and I’ll share it with the rest of you bloggers out there. There are a few suggestions for Windows hosting, but they’re not that helpful if you don’t have admin access to your server:
- Microsoft IIS 7+ web server with the URL Rewrite 1.1+ module and PHP 5 running as FastCGI
- Microsoft IIS 6+ using ASAPI_Rewrite
Luckily we can use the magic of 404 redirection to trick our server into building pretty permalinks. This takes 2 steps. The first is to create our redirect PHP page. Create a file called wp-404-handler.php in your favorite text editor. Paste the code below in, save and close that file, then upload the wp-404-handler.php file to your WordPress blog installation directory.
<?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 = 'wp-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);
?>
The second step is to change our website settings so that all 404 requests go to our newly created page. Most web hosts allow you to enter a path to your custom 404 page. If you don’t see this option, ask your web host and they might be able to set it up for you. For my example I set my 404 error page to: /blog/wp-404-handler.php. And that’s all there is to it! Enjoy your pretty permalinks.
[facebook_ilike]