IIS7 URL Rewrite – Remove www from all urls

I’ve always used the canonical rule template on IIS rewrite to force a www prefix on my urls. Recently I’ve started setting up sites that do the opposite – force no www in the front. Easy enough to do if you have nost headers configured, just select which host name to force to and bam, done.

Now I have a project that is a single web app mapped to an IP. Im running orchard 1.3x on it, so the multi-tenant module allows quick configuraiton of a new site. With the dedicated IP, I don’t have to mess with adding host headers every time I need to create a new site- just set it in DNS and away we go.

The problem with this scenario is – I still had to go into IIS and configure canonical url rules for each new site. What would be nice is to just set one rule that says “strip the www off ANY url coming to this site”.

 

I found the basics for this rule elsewhere on the web, and tuned it to do exactly that:

 

<rule name=”Remove www” stopProcessing=”true”>
  <match url=”(.*)” ignoreCase=”true” />
  <conditions logicalGrouping=”MatchAll”>
    <add input=”{HTTP_HOST}” pattern=”^www\.(.+)$” />
  </conditions>
  <action type=”Redirect” url=”http://{C:1}/{R:0}” appendQueryString=”true” redirectType=”Permanent” />
</rule>

 

Add this in your rules section of the web.config file and you’ll be good to go.