Internal Domain Redirects with ASP.net and C# (C-sharp)
Here is nice little piece of code for issuing an internal domain redirect. Simply insert the code into the global.asax file, which should be found in the root web (www) directory. A commented line explanation is included within. ...and please excuse the improper code structuring.
In this case, we are setting up a redirect from abwebsitedesign.com to www.abwebsitedesign.com in order to ensure proper indexing by search engines.
void Application_BeginRequest(object sender, EventArgs e)
{
//if the requested domain contains this
if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("http://abwebsitedesign.com"))
{
//then return this header response
HttpContext.Current.Response.Status = "301 Moved Permanently";
//and then replace the requested address with the newly defined address
HttpContext.Current.Response.AddHeader("Location", Request.Url.ToString().ToLower().Replace("http://abwebsitedesign.com", "http://www.abwebsitedesign.com"));
}
This also works nicely for telling search engines where a new page can be found (in case you change files, paths, or page names).
void Application_BeginRequest(object sender, EventArgs e)
{
//if the requested address contains this
if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("http://www.abwebsitedesign.com/oldpage.aspx"))
{
//then return this header response
HttpContext.Current.Response.Status = "301 Moved Permanently";
//and then replace the requested address with the newly defined address
HttpContext.Current.Response.AddHeader("Location", Request.Url.ToString().ToLower().Replace("http://www.abwebsitedesign.com/oldpage.aspx", "http://www.abwebsitedesign.com/newpage.aspx"));
}
Pretty simple, yet very necessary for properly, and effectively maintaining SEO.
...
In this case, we are setting up a redirect from abwebsitedesign.com to www.abwebsitedesign.com in order to ensure proper indexing by search engines.
void Application_BeginRequest(object sender, EventArgs e)
{
//if the requested domain contains this
if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("http://abwebsitedesign.com"))
{
//then return this header response
HttpContext.Current.Response.Status = "301 Moved Permanently";
//and then replace the requested address with the newly defined address
HttpContext.Current.Response.AddHeader("Location", Request.Url.ToString().ToLower().Replace("http://abwebsitedesign.com", "http://www.abwebsitedesign.com"));
}
This also works nicely for telling search engines where a new page can be found (in case you change files, paths, or page names).
void Application_BeginRequest(object sender, EventArgs e)
{
//if the requested address contains this
if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("http://www.abwebsitedesign.com/oldpage.aspx"))
{
//then return this header response
HttpContext.Current.Response.Status = "301 Moved Permanently";
//and then replace the requested address with the newly defined address
HttpContext.Current.Response.AddHeader("Location", Request.Url.ToString().ToLower().Replace("http://www.abwebsitedesign.com/oldpage.aspx", "http://www.abwebsitedesign.com/newpage.aspx"));
}
Pretty simple, yet very necessary for properly, and effectively maintaining SEO.
...


0 Comments:
Post a Comment
Links to this post:
Create a Link
<< Home