Redirecting Non-www to Domain Equivalent

A question came up on a blog post of mine on how to redirect a domain name that doesn’t have www in it while retaining the original domain name.  Basically, to have a generic redirect to add on a www if it’s not already there.

For example, how could you have something.com always redirect to www.something.com where something could be any of a number of domain names that you manage.

This is fully possible with URL Rewrite.  Here’s what the config looks like.  If you want a full walkthrough using the setup wizard then refer to my previous post for the details.


<rule name="non-www to www" enabled="true" stopProcessing="true">
<match url=".*" />

<conditions>
<add input="{HTTP_HOST}" pattern="^[^\.]+\.[^\.]+$" />

</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" />
</rule>

This rule watches for all 2 level domains and redirects to the same domain name and tacks on the www to the beginning.  So something.domain.com won’t be redirected, but domain.com will.

Alternately, if visitors will come in through either http or https, you can ensure that this retains the protocol with 2 rules, one for each:

<rule name="non-www to www http" enabled="true" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^[^\.]+\.[^\.]+$" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" />
</rule>
<rule name="non-www to www https" enabled="true" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^[^\.]+\.[^\.]+$" />
<add input="{HTTPS}" pattern="on" />
</conditions>
<action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" />
</rule>

25 Comments

  • Hi Scott,

    How would you generalize this to work with both http and https?

  • Hi Jon,

    Good question. I just updated the blog post to include an example that achieves that. Since there isn't a protocol variable with http or https then it takes two rules to achieve this cleanly.

  • Is it required to add a rewrite? what happens if you leave an "out of the box" configuration, won't www.site.com and site.com type in a url both still work?

    Thanks for your help.

  • Hi Savart,

    Yes, it's fine to leave the default configuration and support both URLs. However for search engine optimization (SEO) it's beneficial to have a single URL, so this rule is for SEO reasons and not for visitor traffic.

  • I was under the impression that for SEO redirects were bad? Because it looks like you are trying to use different URLs to drive traffic, any thoughts on that?

  • I won't claim to be a SEO expert, and it's always changing, but a limited number of redirects aren't bad at all. In fact they help with your page rank because you'll only have 1 site that grows in popularity rather than diluting that across a few names. You may be right that buying a lot of domain names and pointing them to the same location will eventually have a negative impact, although, again, I won't claim to be an expert on that.

    Here's a good link to get started. The first paragraph jumps right into that: http://www.stephanspencer.com/redirects-and-seo-best-practice/

  • I think it useful but i couldn't find what section in the web.config i should add this code?

  • Hi Musa. Good point, it's not clear in the blog. What I sometimes do is to create a dummy rule with the url set as something bogus. That will create a placeholder for you.

    To do it manually, the location is




    {individual rules like the example above}



  • Also, using the rewrite to redirect non-www link to it's www equivalent is useful when the pages have certificates associated with it. The browser will throw a certificate error to the end user if the certificate is for www.example.com and user is on example.com

  • @Lkt. Good point, that does help with not only SEO but also the single URL for the sake of the certificate. As long as the redirect is handled pre-https. It also means that this rule should occur *before* a http to https rule so that you ensure that the www is in place before the certificate is requested.

  • i tried to redirect sandiegolimotime.com to www.sandiegolimotime.com but it shows me an errer ???

  • @Anas, what error message do you receive? At the moment when I test it I don't see the redirect so I assume that you turned it off again since it's not working yet.

  • Scott,

    Thanks for publishing this.

    My impression is that having a status code 301 with the redirect is important for the SEO aspect. I only say this because the SEO stuff I have read specifies creating a 301 redirect. Is there a way to include this in your rule?

    Thank You,
    Randy

  • I just checked the headers on my redirected page and it shows a 301 redirect, so it sounds like nothing else is required.

  • Hi Randy,

    You're right on both parts. The search engines prefer the 301, and the rule also defaults to a 301 if it's not explicitly set.

  • Got it -- thanks again Scott!

  • Oh my hellzbellz. I have spent SO MANY HOURS searching google trying to figure this out, and I was finally able to get it to work THANKS TO THIS BLOG, & the 'my previous post' page. THANK YOU SO MUCH!!!!

    Just a note to anyone out else there who experienced any errors. MAKE SURE YOU HAVE THE BACK SLASHES ON THE CLOSING TAGS! I copied one of the examples above, and it didn't have the backslash on the closing tag, and took me a while to figure out that's why I was getting errors.

    ALSO - for those who have lots of domains on the same server, and you want to JUST EDIT ONE FILE, instead of a whole bunch, the very first example above works, but you do need to stick it in these:



    --> above code <---

    Then close all those tags. You can start with the tag inside a that's already in your applicationHost.config file.

    Which, I had to do a search on the C: Drive of my server, but to save any one time who's looking for it for the SERVER WIDE application of this redirect, here is what that path was on my server:
    C:\Windows\System32\inetsrv\config\applicationHost.cofig

    Hopefully my post saves someone else out there a few minutes or HOURS

    PEACE AND THANKS AGAIN!

  • Hi Lasersightpro,

    Great, glad that helped! Thanks for the update and for the tips for other readers.

    Scott

  • I found the condition for matching only works for single suffix .com type references. It fails to match with .com.au so could be extended to read

    ^[^\.]+\.[^\.]+\.[^\.]+$

  • Thanks Phil. Good point. I live too much in a .com type world and didn't account for that in this rule. That's good to keep in mind to handle the extra level in the URL.

  • I have a mix of domains so I am trying a condition that redirects when the url does not begin www.

  • @Jim. That should do the trick.

  • Works great! Thanks.

  • Thanks OWScott and Phil. So how do i go about if i have 3 urls "domain.com", "domain.com.au" and "domain.co.uk".

    Do i have to create different rules in IIS Url rewrite or can i manage in one rule itself. I have been trying to fix this issue using one rule itself.

    Expertise advice would be greatly appreciated :)

  • Hi Mithun,

    I would probably use the following rule instead. It's an improvement (in most cases) over the version in the original blog post:










    That simply watches for any domain name that doesn't start with www and it adds the www. It maintains the protocol in a single rule too (http or https).

Comments have been disabled for this content.