Over a million developers have joined DZone.

IIS Rewrite Tool – the pain of a simple rule change

DZone's Guide to

IIS Rewrite Tool – the pain of a simple rule change

Free Resource

I had a real pain point with the IIS Rewrite tool today. I wanted to take an out of the box canonical rewrite rule and then modify it slightly to fit my own scenario. This tool is very easy to setup simple, out of the box rules – e.g. in order to get a simple canonical url rewrite (http://test.com to redirect to http://www.test.com then we would do as follows:


You then enter the primary host name – in my case it is www.test.comimage

That is it! The rule is now set up – pretty easy eh! This will redirect any request that isn't http://www.test.com to become http://www.test.com. The following code is added to the web.config file in order to make this work:

So this wasn’t quite right for my setup. I have a main URL that sits with the load balancer – lets say that is www.test.com but I have individual URLs associated with the individual servers lets say an example is http://server1.internal-test.com. I use individual server URLs for server monitoring – Pingdom will constantly hit a specific page on the individual server to test for uptime. The canonical rule I have listed above will redirect the individual URL to the load balanced URL so I had to try and change the built in rule. Firstly I tried to do this via the web.config and adding my own lines. This did not work at all. I could find little documentation about how the rule is constructed so I was basically just "hacking" the rule to try to get it to work.

Then I went and used the built-in editor for the IIS rewrite tool. When I seen the list of rules I double clicked on the rule and got the following screen:


This is where I finally got my editing correct. I had to add a condition to list -


This did not offer my any type of drop down list for the different options I had for condition input – as you can see it is currently selected as {QUERY_STRING}. I didn’t realise I had to change this. But once I had figured this out I changed it to look as follows:


This was the correct rule – it says in pseudo terms – if the URL does not match this format then perform the redirect to the canonical host. This left me with the following web.config entry


This was way too easy to mess up. I would love there to be a way to test my rules in an actual scenario – a sample application that I could pass requests to and see what the response would be. I hope you don’t fall into this rewrite rule trap! Please, IIS team, make it easier to test the rules that I have created.


Published at DZone with permission of Paul Stack, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}