404 error handler while change in permalink

If you get 404 error pages after change permalinks.
step 1: create 404-handler.php in your root folder.

<?php
$default = ‘index.php’;
$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);
?>

step 2: Add the 404-handler.php to the IIS. You will find this setting by right clicking your IIS Manager > Websites > [Site Name] > Properties > Custom Errors > 404 & 404;2 & 404;3 > URL (Requires a ‘/’ prefix in IIS)

Advertisements

One thought on “404 error handler while change in permalink

  1. Lars CMS Meyer says:

    Very nice wordpress trick – this works like a charm. No need to install og buy components to help rewriting, this is all you need. 5 minutes after finding this guide, my wordpress blog is now working with url rewritten permalinks, perfect

    Excellent and easy follow guide, i’d recommend it to others.

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