IIS URL Rewrite Gotcha

When using Visual Studio, and starting a website using F5 or ctrl-F5, the website displayed is the live website and not the localhost website on your development machine. Why?

URL Rewrite Add RuleThe problem can be caused by , for example, using url rewrite to force your website to use the www prefix.

Using the rule template Canonical domain name the code generated in the web.config file will look like rule CanonicalHostNameRule1, shown below:

What it means is that any {HTTP_Host} that does not match www. example .com will be redirected to www. example. com.

This will also include http://localhost:12345/,  the local web address used by visual studio.

Fortunately, many developers system may not have the URL Rewrite module installed (it is a separate download), and therefore will not encounter this problem. Unfortunately, it could take some developers by surprise if this module is downloaded at a later date, and they find that whilst trying to debug their website on their local machine, they find their browser displaying the live website.

One further quirk, is that even when you find the offending rewrite rule in the web.config and remove it, there appears to be some sort of persistent memory that will remember the permanent redirect and the debug session will still be redirected to the live site (http:///www.example.com).

Any comments on how to remove this ‘persistent’ memory of the 301 redirect would be most useful for those confronted by this issue.
<rule name="CanonicalHostNameRule1">
<match url="(.*)" />
<add input="{HTTP_HOST}" pattern="^www\.example\.com$" negate="true" />
<action type="Redirect" url="http://www.example.com/{R:1} redirectType="Permanent" />

This issue can be easily worked around by editing the rule manually to the following:

<rule name="Add WWW prefix">
<match url="(.*)" ignoreCase="true" />
<add input="{HTTP_HOST}" pattern="^example\.com$" />
<action type="Redirect" url="http://www.example.com/{R:0}" redirectType="Permanent" />

Any insights on the pros and cons of the above two rules would be welcomed.

A final note to anyone looking at URL rewrite is that Pluralsight have an excellent course by Scott Forsyth entitled URL Rewrite for Developers.


One thought on “IIS URL Rewrite Gotcha

  1. Nice post and thanks for the call-out.

    You’re right that this can be a problem when the rule is too generic, like in the first example (I realize that I’m to blame since it came from my course as an example). What you did with the 2nd rule works great. Rather than checking for ‘anything that isn’t the right URL’, you’re checking for ‘anything that is exactly the URL without the www’. That more explicit condition ensures that it doesn’t redirect for other domains (like localhost) on the same site (or server if the rule is at the server level).

    Regarding the caching, that’s because of the redirectType=”Permenant” which is a 301 redirect. Web browsers will remember that and they will hang on for dear life. You can test if you’re fighting with the caching (using the term loosely) by using a fresh incognito/inprivate window, or using another browser. If it responds differently then you know that your browser is performing the 301 redirect due to what it has in its history. To remove the cache, you may need to delete your browser history far enough back that you remove the 301 redirect.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s