jdMorgan - 12:17 am on May 28, 2011 (gmt 0)
The first step (and the first thing you need to know) is that you'll need to flush (completely delete) your browser cache before testing, and again after making any change to your server-side code.
Otherwise, your browser will very likely show you a previously-cached local copy of the page, and no HTTP request will be sent to your server. Since this code is server-side, it won't get a chance to execute, and therefore, you'll see no 'fix-up' redirect...
Having tested with an empty cache, let us know what problems remain. The code could do with a bit of cleaning-up, but I don't want to go off-track.