WordPress & Permalinks on a Windows Server

WordPress Logo

WARNING: GEEK MATERIAL ENCLOSED.

Problem: WordPress was designed to run on Apache and not on the Windows IIS server. However, my hosting is a Windows server and I want to use WordPress (which does basically work on Windows IIS) but I also want to use Permalinks (nice URLs, not those “?page=23” type URLS). I’m on a shared hosting plan and my provider has PHP/MySQL but is not going to load a ModRewrite software package.

Basic Solution: I used a php.ini file, a customized 404 Error page, and modified a little bit of WordPress code.

Extended Solution:

My blog is in the root of my domain. I use WordPress as my blogging software and my CMS. You’ll need to alter this code as needed if you have your WordPress installation nested in the domain.

First, I put a php.ini file in the root of my directories (not the wwwroot but the actual root). This php.ini included the following lines:

cgi.fix_pathinfo = 1
cgi.force_redirect = 0

It may take a little bit for this php.ini file to start taking effect (I’m not sure what on the server level kicks it in).

Secondly, I created a customized 404 Error page. Many shared host solutions allow for this now. I put a file called 404Error.php in the wwwroot of my domain and used my control panel to direct 404 Errors to this page (see your specific control panel’s help files for how to do
this). In this page, I put the following code:

< ?php if (strpos($_SERVER['QUERY_STRING'], '.htm')) { echo '< strong>HTTP 404 Error - Unable to find the requested page< br />< br />';
echo '< a href="/index.php" target="_self">Click here for The Roes Garden< /a>< br />< br />';
} else {
$_SERVER['REQUEST_URL'] = substr($_SERVER['QUERY_STRING'], strpos($_SERVER['QUERY_STRING'], ':80')+3);
$_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URL'];
include('index.php');
}
?>

The first part of the “if” statement just sees if they are requesting a standard HTM or HTML file which means its a legitimate 404 error (since I run WordPress as my CMS). The “else” part of the statement does the work I need in redirecting.

Thirdly, I modified some WordPress code. I opened up the file link-template.php found in the WP-Includes folder.

  1. Find the area for the function get_pagenum_link.
  2. Then find the code:
    if ( $qs_match[0] ) {
    $query_string = $qs_match[0];
    $request = preg_replace( $qs_regex, '', $request );
    } else {
    $query_string = '';
    }
  3. Right after this, add the code:
    // Next lines to remove query string if its there because of 404Error
    $phpError = "404;";
    $pos = strpos($query_string, $phpError);
    if ($pos === false) {
    //Do nothing
    } else {
    $query_string = '';
    }
  4. Then find the code within that function:
    $request = preg_replace( '|^index\.php|', '', $request);
  5. Right after this, add the code:
    // Next line is a replacement to handle Paging issues with IIS
    $request = preg_replace( '|^404Error\.php|', '', $request);

Save the file and replace it on the website.

The first two steps allowed me to have Permalinks but after using the “Next Posts” link a couple of times, it got all messed up with 404Error.php garbage that it wouldn’t work past a page 2. The third part cleans all that up and makes it work better.

Search still doesn’t work for me but at least users can scroll through the several years of blogs I have.

Let me know if you have any improvements or if this helped you in any way.


Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.