Charlie Calvert on Elvenware

Writing Code and Prose on Computers

Elvenware

URL Rewrites in IIS

A URL Rewrite enables a webserver to redirect content to the browser without signaling the user that this is happening. This is useful if you want to serve up special content to users of a particular browser or device. The user asks to go to SomePage.html, and you change the content of that page depending on the device, browser or some other criteria. For instance, you might serve up SomePageBig.html for users on a desktop, and SomePageSmall.html for users on a phone. The user does not have to know this is happening, the address bar will only show SomePage.html.

On IIS, URL Rewrites are a feature requiring a separate install. Before trying to install Url Rewrites, you should install the Web Platform Installer, since it gives you access to various tools that you may or may not find useful. Now you are ready to install the URL Rewrite Module. On the download page, look at top right for the link. It is hidden in plain sight.

After the install, you have a new icon in the InetMgr.

Writing a Rule

Our goal is to write a rule that ensures the following:

To get Started, follow these steps:

In the Conditions section choose:

The final step is to say the name of the new rule:

Now it is time to test it:

The rule gets written out as XML to your web.config file, which is found in a directory for your site:

<rewrite>
<rules>
<rule name="MyRule">
<match url="page8.html" />
<conditions>
<add input="{HTTP_USER_AGENT}" pattern="Chrome" />
</conditions>
<action type="Rewrite" url="bar.html" />
</rule>
</rules>
</rewrite>