Professional Microsoft IIS 8
by Kenneth Schaefer, Jeff Cochran, Scott Forsyth, Dennis Glendenning, Benjamin Perkins
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.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access