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.
Get Professional Microsoft IIS 8 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.