2

I see many similar questions written in gibberish that I don't understand:

I'd like to know how to do this using Microsoft's technology ...or just explain to me what those others are talking about and how to use them.

Basically, if someone types "mydomain.com" into the address bar, I want it to resolve to "www.mydomain.com" when the page has finished loading.

EDIT: This is a hosted website, so I can't configure the IIS Server.

Community
  • 1
  • 1
  • 1
    Check the answer here: http://stackoverflow.com/questions/509205/is-there-a-way-to-always-require-or-force-the-www-subdomain-on-a-site – Brian Webster Feb 23 '13 at 03:03
  • Oh, sorry. I can't do that one. This is a hosted website. Should have added that. –  Feb 23 '13 at 03:05

2 Answers2

6
  1. Non www to www redirect
  2. www.yourdomainname.com/default.aspx to www.yourdomainname.com

Now add config tag in web.config

<system.webServer>
<rewrite>
      <rules>
        <rule name="Redirect to WWW" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^yourdomainname.com$" />
          </conditions>
          <action type="Redirect" url="http://www.yourdomainname.com/{R:0}" redirectType="Permanent" />
        </rule>

        <rule name="Default Document" stopProcessing="true">
          <match url="(.*?)/?default\.aspx$" />
          <action type="Redirect" url="{R:1}/" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>

(or) Go with this one:

<rewrite>
    <globalRules>
        <rule name="Redirects to www.domain.com" patternSyntax="ECMAScript" stopProcessing="true">
            <match url=".*" />
            <conditions logicalGrouping="MatchAny">
                <add input="{HTTP_HOST}" pattern="^domain.*(com|net)$" />
                <add input="{HTTP_HOST}" pattern="^(www.)?mydomain2.(com|net)$" />
                <add input="{HTTP_HOST}" pattern="^www.domain.net$" />
            </conditions>
            <action type="Redirect" url="http://www.domain.com/{R:0}" />
        </rule>
    </globalRules>
</rewrite>
coder
  • 13,002
  • 31
  • 112
  • 214
  • 1
    I have a feeling that this is the correct answer in general, but it seems like he probably doesn't have the ability add IIS Rewrite module due to his hosting environment. But, if he does have that module, this is best – Brian Webster Feb 23 '13 at 03:18
  • Yeah, my VS2010 project is telling me `` is an invalid child element. What section of the `web.config` file does the second example go in? –  Feb 23 '13 at 03:22
  • Cool! Even though VS2010 complained, I still uploaded that version, and it worked, too. –  Feb 23 '13 at 03:31
3

This is kind of a funny solution. I am only suggesting it because of the limitations which you described. It's better to do it in IIS or using an HTTP Module like those other answers are suggesting. However, this would also work, it's just not a very good way to accomplish it.

You can put this code into your Page Init Event handlers (or Init Handler for your master page).

If Request.RawUrl.StartsWith("http://mydomain") Then
  Response.Redirect(Request.RawUrl.Replace("://", "://www."))
End If

Where mydomain is like mydomain.com without the www.

It checks to see if the URL doesn't have WWW where WWW "should" be. If it isn't there, redirects the user to a version of that page that has WWW in the correct spot.

Brian Webster
  • 30,033
  • 48
  • 152
  • 225
  • Thanks Brian. That's what I was hoping I did not have to do, but I did not know how to ask that question. Any tips for how the question should have been worded so others in the ASP.NET world can find it in the future? –  Feb 23 '13 at 03:32
  • 1
    This answer of mine is a good solution if you don't want to do an HTTP Module and you don't have access to the IIS config to install the Rewrite Module. It's best if you have a master page where you only have to put the code in once, but it's not the end of the world if every page needs the code. I'd adjust your question title to "without access to IIS config". That will help people a bit, I think – Brian Webster Feb 23 '13 at 03:37