I need to redirect my website when user enters URL without non www, and it should be redirect with www.
Example: abc.com to www.abc.com
And also i need to support subdomain url too.
Example: abc.xyz.com to www.abc.xyz.com
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<rewrite>
<rules>
<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>
</rules>
</rewrite>
</system.webServer>
You can also catch this in global, Applicaton_BeginRequest
:
string url = HttpContext.Current.Request.RawUrl;
if (!url.StartsWith("www.")) {
Response.Redirect("www." + url);
}
Edit: This question shows that 302 is returned by Response.Redirect.
As someone has answered there, Response.RedirectPermament
can be used with .NET 4.0
and this will return 301.
Weird thing is. I use Response.Redirect
on one of my sites in .NET 4.0
and it returns 301
just fine.