The URL Rewrite IIS extension gives IIS similar URL rewriting capabilities to Apache mod_rewrite, but being Microsoft you get a nice GUI to help. However I couldn’t get it to work, somehow I stumbled across a solution that fixed it for me…
The extension is available as part of the Web Platform Installer so, because I like installing things, I’ve had it installed for quite some time. Back in February I needed to use a redirect for a website, we were setting up a new website but needed to redirect traffic to the current site – for a little while. Quite keen to give my new toys a go I initially tried to get the HTTP Redirect module to redirect my traffic. It looked pretty easy, fill in the boxes and any traffic coming to the site would be redirected with the status code I set.

Unfortunately I couldn’t get the HTTP Redirect to redirect but one of the responses to that post suggested that another option was to use the URL Rewrite module and helpfully provided me with an example. I tried the module but again couldn’t get the rewrite / redirect to work, in the end I resorted to adding a default.aspx with
<%Response.Redirect(“http://www.newurl.com”)%>
Which wasn’t a great solution as it only worked if going to the root of the directory, but for a quick fix was fine. However yesterday I needed to do the same thing again but wasn’t prepared to use such a hacky fix. Somehow I discovered that the redirect worked if I had other files in my website. I had been trying to get the redirect / rewrite to work with only a web.config file in the root, no other files, no other folders just the web.config with this redirect:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="olddomain.com" />
</conditions>
<action type="Redirect" url="http://www.newdomain.com" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
<httpRedirect enabled="false" destination="http://www.newdomain.com" exactDestination="true" />
</system.webServer>
</configuration>
Frustratingly this wasn’t working, when I ran the Test pattern tool with the regex (.*) and gave it anything it matched, which is just what I wanted – any traffic that made it to this site should be redirected to newdomain.com – so why wasn’t it working when I used it on the site?
I wish I could explain why this only works when I have more than just the web.config in the folder, but it just does – maybe the website isn’t considered a website without there being at least 1 web page, maybe just a web.config alone isn’t enough – I don’t know. If anyone else knows, or can explain what’s going on here and why I’d be very interested to hear about it in the comments. In the meantime I’m posting this in case it helps anyone else with this gotcha!
da05ea08-d9b1-4bcf-b27b-c3d37b667f50|0|.0
Permalink |
Comments (0)