Posts By Category

Posts By Date

Resources:

C# Books
ASP.NET Books DotNet4All








If you like to support this site, feel free to make a donation to support improvements.

Thank you!

Monetize Your Blog

How I handled 301 redirects to avoid duplicates in my SE ratings

If you have done SEO work, you know that Search Engines would penalize my site if both http://www.mycsharpcorner.com and http://mycsharpcorner.com/ requests returned a 200 OK code in the HTTP header. Why? Because from a Search Engine's spider point of view, there are 2 sites, one with the www prefix, and one without it, yet in reality they the same site. So what’s wrong if the Search Engine thinks

that way? Well, it leads it to think one of the two sites is showing duplicate content! This would raise a flag with Google’s “duplicate filter” as its ears would point up to the smell of duplicate content. What follows is that your page rankings get hurt once you are tagged a duplicate. Moreover, you suffer rankings split between two sites rather than one.

 

The solution for this is to do a 301 Redirect. I looked around the net, and there seems to be many articles written about 301 Redirects. Here is a solution that shows up a lot:

 

<script>
protected void Page_Load(object sender, EventArgs e)
{
   Response.Status =
"301 Moved Permanently" ;
   Response.AddHeader(
"Location" ,"http://www.mycsharpcorner.com" );
}
</script>

 

For my ASP.NET site, I needed something general, not in each page load event, but for the whole site, so I don’t have to worry about it ever again. So here is what I’ve done:

 

In my Global.asax file (You can add this to your site from Visual Web Developer or Visual Studio by selecting File->New), In the Application_BeginRequest event, I added a call to the following code:

 

if (HttpContext.Current.Request.Url.ToString().ToLower().Contains(

    "http://mycsharpcorner.com"))

{

    HttpContext.Current.Response.Status =

        "301 Moved Permanently";

    HttpContext.Current.Response.AddHeader("Location",

        Request.Url.ToString().ToLower().Replace(

            "http://mycsharpcorner.com",

            "http://www.mycsharpcorner.com"));

}

 

The above code allows 301 redirects from any entry point to the site. Next I tested my site using this free tool: http://www.rexswain.com/httpview.html , and when I typed http://www.mycsharpcorner.com I got a 200 OK in the HTTP header, but if I type http://mycsharpcorner.com/ then I got a 301 Moved Permanently code, where before the modification, I got 200 OK on both requests. The new result means the Search Engines will consider both urls to point to the same site, so all page rankings will add up regardless of the entry point. Google spider, are you reading this?

kick it on DotNetKicks.com

Feedback

Posted on 9/24/2011 4:03:20 PM

The solutiion looks good. But the very first code that you have written in page_Load event is wired inside a script tag.
Will this really work? I think it will throw an error.
The second one (global.asax) is a better option.

Posted on 9/23/2011 4:02:34 PM

It is good that we have this coding available.
But can we do the same at Server level itself (IIS 7 / websitepanel), like it is available in Apache server through .htaccess? Can we use web.config for the same?

Posted on 9/14/2011 2:08:59 PM

Thank you for this info! What I like about this method VS configuring a redirection in IIS, is that iis is removing the trailing / at the end of the url we type. So, it redirects to our 2nd site without the final /, and after, our site is doing a second redirect to add the /. With the global.asax method, I can redirect directly to my other url with the /, there is no dual redirect 301, that is more seo friendly!

Posted on 11/5/2010 6:14:50 AM

Very Interesting Article

Posted on 6/8/2010 2:21:14 PM

Hello, is there a way that we can could do this re-direction using web.config file?

My website was developed in 2002, it was develobed in .aspx & now I wanted it to be redirected it to .html. As if now I've done header redirection (page by page) & there about 200 files. Please advice me. Appreciate your help in advance. Thanks

Posted on 2/8/2010 12:30:44 PM

Thank you, It's really helpful to me...

Posted on 8/8/2009 3:13:47 AM

While you are right, you should also create a google webmaster tools account and define the correct URL. It's more of a canonicalization issue, only kind of a duplicate content issue.

Posted on 5/13/2009 5:41:45 AM

Hi,

I need to redirect some of my old sites to new domain and is using frame in old site to locate the files from the new site.
Instead of that now I want to redirect it to new site if some one take teh old url and I used above code in the new site but its showing "Firefox has detected that the server is redirecting the request for this address"
Is there a solution for this. Please help me to find solution.

Thanks

Posted on 5/11/2009 10:16:21 AM

Excellent artilce on SEO

Posted on 2/21/2009 5:30:48 PM

I wonder if doing this may mess up the whole site.

Posted on 2/12/2009 3:46:15 PM

Url are case sensitive and http://www.domain.com/Page.aspx and http://www.domain.com/page.aspx are treated differently. Your method here redirects pages to a lower case version.
TRY THIS:

string authority = Request.Url.Authority;
if (authority.IndexOf("www.") != 0)
{
Response.StatusCode = 301;
Response.Redirect("http://www." + authority + Request.Url.AbsolutePath, true);
}

Posted on 7/15/2008 3:26:10 AM

Thanks!

Posted on 9/19/2007 6:52:35 AM

Great article, thanks.

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 


Copyright © 2007 Yousef Mannaa. All material on this site is copyrighted.
Do not publish or reproduce any of this material without written permission from the Author