What's the address of your website? www.domain.com or domain.com?

There are two camps on the subject of the www subdomain. One believe it should be enforced (www.yes-www.org) and the other (no-www.org) that it should be removed. They are both right.

What's important is that there is only a single canonical address to your website – with or without www.

The web.config makes it easy for us to either enforce or remove the www subdomain using URL rewrites. There are many examples online on how to do this, but they all share 2 fundamental flaws. The rules have a direct dependency to the domain name and they don't work with both HTTP and HTTPS.

So let's see if we can create generic URL rewrite rules that can be used on any website without modifications.

Your server needs to have the URL Rewrite module installed. Chances are that it does already. Azure Websites does and so does all of my other hosting providers.

Rewrite rules need to be placed inside the <rewrite> element in web.config:

<system.webServer>
  <rewrite>
    <rules>
      <!-- My rules -->
    </rules>
  </rewrite>
</system.webServer>

So here are 2 rules that works on all domains and on both HTTP and HTTPS.

Remove WWW

This rule redirects any incoming request to www.domain.com to domain.com while preserving the HTTP(S) protocol:

<rule name="Remove WWW" patternSyntax="Wildcard" stopProcessing="true">
  <match url="*" />
  <conditions>
    <add input="{CACHE_URL}" pattern="*://www.*" />
  </conditions>
  <action type="Redirect" url="{C:1}://{C:2}" redirectType="Permanent" />
</rule>

Enforce WWW

This rule redirects any incoming request to domain.com to www.domain.com while preserving the HTTP(S) protocol:

<rule name="Enforce WWW" stopProcessing="true">
  <match url=".*" />
  <conditions>
    <add input="{CACHE_URL}" pattern="^(.+)://(?!www)(.*)" />
  </conditions>
  <action type="Redirect" url="{C:1}://www.{C:2}" redirectType="Permanent" />
</rule>

So there you have it. It's easy once you now how.

For more info on the URL Rewrite Module, see the Configuration Reference.

Comments

Stefan Kip

I use these rules, which do more than just remove 'www.': &lt;!-- SEO rules (from: http://www.seomoz.org/blog/what-every-seo-should-know-about-iis#chaining) --&gt; &lt;!-- SEO | Section 1 | Whitelist --&gt; &lt;rule name="Whitelist - Resources" stopProcessing="true"&gt; &lt;match url="^(?:css/|scripts/|images/|install/|config/|umbraco/|umbraco_client/|base/|webresource\.axd|scriptresource\.axd|__browserLink|[^/]*/arterySignalR/.*)" /&gt; &lt;conditions logicalGrouping="MatchAll" trackAllCaptures="false" /&gt; &lt;action type="None" /&gt; &lt;/rule&gt; &lt;!-- SEO | Section 2 | Rewrites (chaining) --&gt; &lt;rule name="SEO - Remove default.aspx" stopProcessing="false"&gt; &lt;match url="(.*?)/?default\.aspx$" /&gt; &lt;conditions logicalGrouping="MatchAll" trackAllCaptures="false"&gt; &lt;add input="{HTTP_METHOD}" pattern="GET" /&gt; &lt;/conditions&gt; &lt;action type="Rewrite" url="_{R:1}" /&gt; &lt;/rule&gt; &lt;rule name="SEO - Remove trailing slash" stopProcessing="false"&gt; &lt;match url="(. )/$" /&gt; &lt;conditions logicalGrouping="MatchAll" trackAllCaptures="false"&gt; &lt;add input="{HTTP_METHOD}" pattern="GET" /&gt; &lt;add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /&gt; &lt;add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /&gt; &lt;/conditions&gt; &lt;action type="Rewrite" url="_{R:1}" /&gt; &lt;/rule&gt; &lt;rule name="SEO - Lower case" stopProcessing="false"&gt; &lt;match url="(.*)" ignoreCase="false" /&gt; &lt;conditions logicalGrouping="MatchAll" trackAllCaptures="false"&gt; &lt;add input="{HTTP_METHOD}" pattern="GET" /&gt; &lt;add input="{R:1}" pattern="[A-Z]" ignoreCase="false" /&gt; &lt;/conditions&gt; &lt;action type="Rewrite" url="_{ToLower:{R:1}}" /&gt; &lt;/rule&gt; &lt;!-- SEO | Section 3 | Redirecting --&gt;

Stefan Kip

Stefan Kip

Sorry, I missed some stuff, previous comment can be deleted! Also I made a gist instead of posting it all here :p https://gist.github.com/kipusoep/8179217

Stefan Kip

Mads Kristensen

@Stefan, that's brilliant. I like that you use rewrites to force URLs to be lower cased. I'm gonna do that too. Thanks for sharing!!

Mads Kristensen

Nick

How is what you're doing to achieve this a better alternative to the default rule created when you use "Add Rule(s)" dialog in IIS and choose "Canonical domain name"?

Nick

Mads Kristensen

@Nick, it might be the same. You don't always have access to the IIS management tools though

Mads Kristensen

Kal

This breaks if you are using an ip address url while debugging. The method I found via Google is to have 2 rules that check the SERVER_PORT_SECURE header and depending on the value set http or https. Since the domain name is part of the test it is ignored when an ip address url is used.

Kal

Randy Martin

@Stefan - the redirect action for your https rewrite should actually say &lt;action type="Redirect" url="https://{C:1}/{R:2}" /&gt; You have http instead of https in the gist you did.

Randy Martin

Silk Rule

I have a website like www.abc.com. Now I want is that someone types like www.a.abc.com or www.test.abc.com whatever before abc.com. I want my users to redirect to www.abc.com.

Silk Rule

Comments are closed