Using IIRF URL Rewriting on IIS6 with WordPress

This is a WordPress blog hosted on IIS6, which isn’t always the smoothest combination. One common WordPress feature not supported on IIS6 is clean URLs requiring permalinks to look like this:

I wanted to get rid of the ‘index.php’ from the URL and had some experience doing URL rewriting using the excellent open source project Ionic’s ISAPI Rewrite Filter. IIRF is a ISAPI extension you can install on your IIS6 web servers to achieve mod_rewrite (from Apache) style URL rewriting. ISAPI filters on IIS6 are invoked prior to a request being directed to ASP.NET or PHP, so it’s great for removing file extensions like .aspx and .php. WordPress runs almost everything through the index.php file, so it’s not that complicated to rewrite all requests to remove the page name from the URL. To setup IIRF, follow the instructions in the readme included with the download.

Here’s my rule file:

RewriteCond %{HTTP_HOST} ^(www\.john-sheehan\.com).*$ [I]
RedirectRule ^/(.*)$$1 [I,R=301]

RedirectRule ^/blog/index\.php/(.*)$ /blog/$1 [I,R=301]
RewriteRule ^/blog/(?!index\.php|wp-|xmlrpc)(.*)$ /blog/index.php/$1 [I,L]

The first two lines remove the www. from any requests. If you hit my blog with www. at the beginning of the URL, you’ll get a permanent redirect (301) to the same page without the www. The 301 redirect is to indicate to Google and other search engines that their indexes should be updated with the new location. The [I] flag specifies that it should do a case-insensitive match.

The third line takes care of issuing a 301 Redirect for all the existing links that include the index.php. I have a lot of backlinks out there and I obviously didn’t want any to break.

The last line has some serious regex-fu in it, so if you’re not familiar with regex, here’s a summary. The rule matches any incoming request that starts with /blog, not followed by index.php, wp- (we don’t want to rewrite any admin or content requests) or xmlrpc (if you exclude this, Windows Live Writer won’t work with your blog). The (.*) captures everything after /blog/. This capture is referred to in the second half of the rule with $1 that tells IIS which URL to serve up instead. The [L] flag tells IIRF to not process any more rules (preventing a loop where the rewritten URL would redirect forever).

Lastly, you’ll need to configure your WordPress Permalink structure like so: