Getting Permalinks Working in WordPress on IIS

This is a frequent question for most IIS users on how to get permalinks to work in WordPress, it's fairly simple really once you know how of course. So the problems are, you're using IIS so using mod_rewrite won't work, you're on a shared server so installing a mod_rewrite alternative for IIS is not available either. You can have URLs with a index.php in them (for example http://www.yourdomain.com/index.php/2009/03/17/getting-permalinks-working-in-wordpress-on-iis) but they're pretty ugly looking and doesn't fit into the URL standards (in other words, it's just nasty).

WordPress Codex has a very useful page about working with permalinks, there's one solution that uses 404 error pages, however when you look at what it does it's very slow and again very nasty. The 404 error solution makes the server try to find the page, it doesn't exist so the server directs the user to the 404 error page, the 404 error page then does it's own working out to figure out where the user should be going, and then it would make it's own HTTP to the correct URL.

This example would work great if only it didn't make a HTTP, so what can we do with this example to make it more efficient.

  1. <?php
  2. $qs = $_SERVER['QUERY_STRING'];
  3. $pos = strrpos($qs, '://');
  4. $pos = strpos($qs, '/', $pos + 4);
  5. $_SERVER['REQUEST_URI'] = substr($qs, $pos);
  6. $_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI'];
  7. include('index.php');
  8. ?>

* Put the above 404 error script in the base of your WordPress directory.

* Set your site's 404 Error Page to the file above

* Setup your permalinks in WordPress -> Options -> Permalinks

Because it's including the index.php file into the script, it's utilising the functionality that is already present in WordPress.

Resources
* Pretty WordPress Permalinks on IIS
* WordPress Codex Permalinks
* 404 Error Solution

This entry was posted on Tuesday, March 17th, 2009 at 8:54 pm and is filed under Internet, Programming. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply