Response for C#.Net Permanent Redirect

I changed domains and wanted all my pages to redirect to the new domain seamlessly, so I implemented the below code in the master pages:

if (Request.Url.ToString().Contains(“domain you don’t want anymore”))
{
Response.Status = “301 Moved Permanently”;
Response.AddHeader(“Location”, Request.Url.ToString().Replace(“domain you don’t want anymore”, “new domain”));
}

This worked for most pages but some pages (particularly those that were being directed from Google) were not being redirected to the new domain. So I added an additional line:

if (Request.Url.ToString().Contains(“domain you don’t want anymore”))
{

Response.Status = “301 Moved Permanently”;
Response.AddHeader(“Location”, Request.Url.ToString().Replace(“domain you don’t want anymore”, “new domain”));
Response.Redirect(Request.Url.ToString().Replace(“domain you don’t want anymore”, “new domain”));

}

And all is well for now…

About admin