Without www to URL with www. (301 Redirect)

Ranjeet_8
Posted by Ranjeet_8 under ASP.NET category on | Points: 40 | Views : 1811
Add this code on your Global.asax

protected void Application_BeginRequest(object sender, EventArgs e)
{
string Domainloc = Request.Url.Authority;
Domainloc = Domainloc.Substring(0, 9);
if (Domainloc != "localhost")
{
if (!Request.Url.Authority.StartsWith("www"))
{
string MainUrl = Request.Url.Authority + Request.Url.PathAndQuery;
string url = string.Format("{0}://www.{1}{2}",
Request.Url.Scheme,
Request.Url.Authority,
Request.Url.PathAndQuery);
Response.Status = "301 Moved Permanently";
if (MainUrl.ToUpper().Trim() == "YOUR_DOMAIN_NAME.COM/DEFAULT.ASPX")
{
string urlMain = string.Format("{0}://www.{1}",
Request.Url.Scheme,
Request.Url.Authority);
Response.AddHeader("Location", urlMain);
}
else
{
Response.AddHeader("Location", url);
}
}
}
}

Comments or Responses

Posted by: Samarmir on: 11/27/2012 Level:Starter | Status: [Member] | Points: 10
please provide just a few lines of explanation.

Login to post response