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: http://john-sheehan.com/blog/index.php/net-cheat-sheets/

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 ^/(.*)$ http://john-sheehan.com/$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:

image

Posted January 11th, 2009 2:15 PM
Read more posts about Tips.

Comments
Link

  • rdevarona
    John,

    Thanks for taking the time to document. I'm always amazed at how many other people are going through/have just gone through the same exact scenario I'm going through. This post is very timely. One question though... with the custom structure set tot he postname, what happens if there are duplicate post names?
  • I don't believe WordPress allows duplicate slugs.
  • everything is ok, but image links is not working in wordpress.
    I found only ModRewritePro is working fine with rewrites, but that's commercial.
    I can't understand the peoples that the big thing is FREE (the wordpress engine), but the small one is not free.
  • What folder are your images in?
  • THANK YOU SO MUCH !!!

    Was hunting for this for so long.
  • John, this is GREAT. Really appreciate someone else doing the regex work—that's really not my forte. Cheers and thank yous!
  • I have a client running Wordpress as a CMS...so there is no /blog/ directory. So a few simple modifications and John's code is now money for the root directory as well:

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

    (Hint: just drop "/blog" from the expression). Yes, this is simple, but maybe some of your readers could find it useful.
  • Thanks Ben!
  • Alternatively, a small change made Wordpress'e original htaccess file if you can use:

    RewriteCond %{HTTP_HOST} !^(tuncay\.kinali\.net) [NC]
    RewriteCond %{HTTP_HOST} !^$
    RewriteRule ^/(.*)$ http://tuncay.kinali.net/$1 [R,L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^/(.*)$ /index.php/$1 [L]

    Sorry for my bad English. It's Google Translate's mistake :)
  • Mike L
    How would you set this up to use multiple domains on the server in different iis sites?
  • You can handle multiple domains in different sites with IIRF. With IIRF v1.2 it is possible, with IIRF v2.0 it is easy. Check the IIRF forums for assitance. http://iirf.codeplex.com/Thread/List.aspx
  • Stu
    This is excellent! once i had ftp access to the dll location from my kind web host i was able to get the ini file up with the 2 rules above and it worked straight away with any custom structure i care to use. Thank you so much for posting this! I will now endeavour to understand how rewrites work...
  • Great work John!

    Unfortunately I've got my blog into a crisis and now I can't logon. Whatever link I click I only get returned to my home page.

    Any idea what I'm doing wrong? Help!!

    http://caspianit.co.uk

    Simon



    RewriteCond %{HTTP_HOST} ^(www\.caspianit\.co.uk).*$ [I]
    RedirectRule ^/(.*)$ http://caspianit.co.uk/$1 [I,R=301]

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

    I just cut n' pasted the code from the comment by Tuncay KINALI - Bingo!

    Working perfectly - many thanks.

    Simon
  • davidpenrose
    Getting the following error when using these rules:

    CGI Error
    The specified CGI application misbehaved by not returning a complete set of HTTP headers.

    I'm able to to pages under the wp- directories.

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

    If replace the above with:

    RewriteRule . /index.php [I]

    Public pages are then visible, albeit missing all styling from css. However, then unable to reach pages under wp- directories.

    Environ:
    IIS6
    Iirf.dll v2.0.1.15

    What am I doing wrong?
blog comments powered by Disqus