I see many similar questions written in gibberish that I don't understand:
- Redirecting non-www URL to www using .htaccess
- Redirecting URL without www to www
- Redirect Non-WWW to WWW URLs
- rewrite url to non www multiple sites
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.
- Non www to www redirect
- 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>
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.