Wednesday, July 25, 2012

IIS 7 URL Rewrite debugging

I needed to redirect a url from one domain to another while also removing part of the path, i.e.

I used IIS 7's "URL Rewrite" module, and the test tool showed that my regex appeared to work correctly, but nothing was actually being redirected. I finally turned on "Failed Request Tracing Rules", added a rule to catch all 404 errors, and was able to look through the resulting log and quickly determine the error.

Here's the setup:

The log files are in the regular log files directory, under the "FaileReqLogFiles" directory by default. Open the  file with or similar to the name "fr000001.xml", and search for your redirection rule name. You should see lots of debugging details to help you determine why the redirect didn't work. In my case, I was trying to match the slash before the folder as well as after it, but IIS was only looking at the part of the path after the domain name, so it never matched.

And here's the rule setup to redirect from a domain to another domain while replacing part of the path, in case anybody needs it:

Under "Match URL --> Pattern", add a regex similar to this:


This gets the url that contains the folder name with a slash after it, then creates a backreference for anything else that follows it, that you can refer to as "{R:1}".

Under "Redirect URL", enter:{R:1}