O'Reilly logo

Professional Microsoft IIS 8 by Benjamin Perkins, Dennis Glendenning, Scott Forsyth, Jeff Cochran, Kenneth Schaefer

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Common Rules

URL Rewrite is extremely flexible and has diverse usages, but certain questions tend to come up more frequently than others. Following are some examples of common rules that you can use as a base when creating your own rules.

Redirecting Non-www to www (Canonical Hostnames)

Forcing your page to redirect from domain.com to www.domain.com is a common practice for SEO. As mentioned previously, you can use the Canonical Domain Name template to create a rule that will redirect all traffic for a website to a single URL. The following example can be placed at the server or site level. It will cause a redirect to www.localtest.me if the domain name is localtest.me:

<rule name="Redirect localtest.me to www">
   <match url=".*" />
   <conditions>
      <add input="{HTTP_HOST}" pattern="ˆlocaltest\.me$" />
   </conditions>
   <action type="Redirect" url="http://www.localtest.me/{R:0}" />
</rule>

The other common option is to reverse the direction if you prefer to drop the www from the URL:

<rule name="Redirect localtest.me from www">
   <match url=".*" />
   <conditions>
      <add input="{HTTP_HOST}" pattern="ˆwww\.localtest\.me$" />
   </conditions>
   <action type="Redirect" url="http://localtest.me/{R:0}" />
</rule>

You can test by applying the first rule and visiting http://localtest.me. Your page should redirect to http://www.localtest.me. The second rule is the opposite; it will redirect from http://www.localtest.me back to http://localtest.me.

Note
See the upcoming “Adding HTTP_PROTOCOL” section to ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required