Point Multiple Domain Names to One Website: Server Side Redirect in ASP.NET
Richard Harris
DevOps, Online & Mobile at TD Bank Group #devops #agile #cloud #java #js #csharp
Introduction
Chances are, there are several different URLs that point to the same content on your website. For example, the URLs https://yoursite.com, https://yoursite.com/default.aspx, https://www.yoursite.com, or https://www.yoursite.com/default.aspx are all likely valid URLs that results in the same content, namely the homepage for yoursite.com. While having four different URLs reference the same content may not seem like a big deal, it can directly impact your website's search engine placement and, consequently, it's traffic. To a search engine, those four different URLs represent four different pages, even though the all produce the same content.
To understand how allowing duplicate URLs in your website can affect your search engine placement, first understand that search engines base a page's placement in the search results based, in part, on how many other websites link to the page. Now, imagine that there are 1,000 web pages from other websites that link to your homepage. You might conclude, then, that a search engine would rank the importance of your homepage based on those 1,000 links. But consider what would happen if 25% of those links linked to https://yoursite.com, 25% to https://yoursite.com/default.aspx, and so on. Rather than your homepage reflecting 1,000 inbound links, instead the search engine assumes there are only 250 links to https://yoursite.com, only 250 links to https://yoursite.com/default.aspx, and so on. In effect, redundant URLs can dilute your search engine ranking.
A key tenet of search engine optimization is URL normalization, or URL canonicalization. URL normalization is the process of eliminating duplicate URLs in your website. This article explores four different ways to implement URL normalization in your ASP.NET website. The code provided at the bottom handles adds consistency to your website URLs via: i) HTTP to HTTPS re-direction ii) Always have www iii) 301 Permanent Redirects from non-primary domains (i.e. .ca -> .com).
Content Duplication
Duplicate content on your own website, where different web addresses ("URLs") on your site display identical content, can lead to problems in your website's performance in the search engine. When you have two different addresses pointing to the same page, like www.example.com/offers.html and example.com/offers.html, many search engines (or so we are led to believe) will treat those two URLs as two separate pages and websites.
Removing content duplication makes your website more search engine friendly. To imporve your search engine ranking on Google, you want everyone linking to you to reference a single domain, not a single site loading with multiple domains. If people are liknking to 3 different domains, none of them will appear to be as important as they could have had all the links been attributed to a single web address. If you have already pointed all your domains to your website this way, not to worry, this is why our code includes the 301 Permanent Redirect, which is a simple way to tell search engines that all 3 domains point to the same site, and to add up all the back links together and associate them as a single site.
The way to deal with it is to redirect all pages using one form of the web address ("URL") to the other form, using what is known as a "301" (or "permanent") redirection code. When the search engines see the redirection along with the 301 code, it will realise that the page specified has been permanently relocated to another address.
Issuing Permanent Redirects From ASP.NET (Application_BeginRequest in Global.asax)
void Application_BeginRequest(object sender, EventArgs e)
{
// Basic SSL Redirection:
/*
if (!Request.IsSecureConnection)
{
Response.Redirect("https://" + Request.Url.Host + Request.RawUrl);
}
*/
var baseURL = "https://www.domain.com";
// Avoid redirection on localhost (local development)
if (Request.Url.Authority.StartsWith("localhost", StringComparison.Ordinal))
return;
// Example - If URL already https://www.domain.com then do nothing
if (Request.Url.Scheme == "https" && Request.Url.Authority == baseURL)
return;
// ASP.NET 4 Solution:
/*
var url = string.Format("{0}://www.{1}{2}",
Request.Url.Scheme,
Request.Url.Authority,
Request.RawUrl);
Response.RedirectPermanent(url, true);
// If you are not using ASP.NET 4 you will have to write a few more lines of code
// to issue a permanent redirect (see bellow). Do not use Response.Redirect as
// that issues a temporary redirect and won't cause the search engines to update
// their indexes.
*/
// ASP.NET 3.5 Solution:
if (!HttpContext.Current.Request.Url.ToString().ToLower().Contains(baseURL))
{
HttpContext.Current.Response.Status = "301 Moved Permanently";
Response.StatusCode = (int)System.Net.HttpStatusCode.MovedPermanently; // 301
Response.AddHeader("Location", baseURL + Request.RawUrl.ToString());
Response.End();
}
}
References
- asp.net - How to redirect everything via Web.config or C# to https://www. version of the site? - Stack Overflow
- 301 redirect in asp.net 4.0 - Stack Overflow
- How to Setup 301 Redirect in classic ASP, ASP.Net and IIS 6.0 - InetSolution
- ASP.NET Global.asax Example - Dot Net Perls
***
- Techniques for Preventing Duplicate URLs in Your Website
- How to Point Multiple Domain Names to One Website: And How to Avoid Search Engine Problems When Doing So
- How to Create a Search Engine Friendly Website