ASP.NET

Redirect domain Without www to With www in ASP.NET, BlogEngine.NET

It is possible to access https://www.dotnetstuffs.com,  and http://dotnetstuffs.com from the server after we host the domain. The search engines consider it as 2 different sites.

From an SEO standpoint, this will be considered duplicate content on the web, and search engines will penalize it.

For quite some time, in my blog, there was the issue that my domain renders a different page when requested with URL as http://dotnetstuffs.com and requested with www.dotnetstuffs.com.

This was causing issues of duplicated content and SEO issues which further caused rejection from the Adsense program.

I was under impression that this redirection will normally be taken care of by the hosting provider.

When contacted them I was informed that this redirection is the responsibility of the site developer.

I googled and found many suggestions like adding a .htaccess file to do a 301 redirect etc.

But that all were not of use to rectify my issue. The further analysis concluded to do the redirection through my site coding, normally as we do for page redirection.

URL Redirection in ASP.NET

This should be done in the Global.asax file.

Since my site is developed in asp.net, I need to do the asp.net page redirection when receiving the URL request.

With asp.net 4.0, there is also permanent redirection of pages which protects you even from losing search engine page rankings.

In this article, I try to explain the URL redirection in asp.net. Hope this article will help you.

There are different ways to do this URL redirection in the ASP.Net application.

One approach is to use the Application_BeginRequest event of Global.asax to redirect all incoming requests to do a permanent redirection with status code “301 Moved permanently“.

The below code helps you to do that. Go to Global.asax file and insert the code in the event Application_BeginRequest

void Application_BeginRequest(object sender, EventArgs e)
{
   string requestedUrl = HttpContext.Current.Request.Url.ToString().ToLower();
   string requestedDomain = "http://dotnetstuffs.com";
   string correctDomain = "https://www.dotnetstuffs.com";

   if (requestedUrl.Contains(requestedDomain))
   {
      HttpContext.Current.Response.Status = "301 Moved Permanently";
      HttpContext.Current.Response.AddHeader("Location", 
               requestedUrl .Replace(requestedDomain, correctDomain));
  }
}

Redirect domain With www to Without www in ASP.NET

Redirecting a domain With www to Without www in ASP.NET is similar as above explained.

The only difference is to change the requested domain and correct domain address as below. The rest of the code is exactly the same as above.

string requestedDomain = "https://www.dotnetstuffs.com";
string correctDomain = "http://dotnetstuffs.com";

Another approach of doing Non-www to www Redirection in ASp.NET is explained below

How To Redirect Non-www to www in Asp.net

keep reading to know another step to redirect Non-www to www for your domain in asp.net using the web. config file. The same can be done for your classic asp applications also.

The approach is to do a 301 Redirect of a non-WWW domain to WWW domain in ASP.NET which is explained here.

For that, go ahead and edit and save your web.config file by adding the following<rewrite> xml segment under the <system.webserver> section,before closing of </system.webserver>

<rewrite>
   <rules> 
      <rule name="Redirect from non www to www in ASP.NET" 
             patternSyntax="ECMAScript" stopProcessing="true"> 
        <match url=".*"></match> 
        <conditions> 
          <add input="{HTTP_HOST}" pattern="^dotnetstuffs.com$"></add> 
          <add input="{HTTPS}" pattern="off"></add> 
        </conditions> 
        <action type="Redirect" url="https://www.dotnetstuffs.com/{R:0}" 
           redirectType="Permanent" appendQueryString="true"></action>  
      </rule>  
  </rules> 
</rewrite>

Now on, your domain without www or with www will direct to with www url.

Summary

I hope this article helps the developers struggling with ASP.NET  URL redirection from non www domain to www domain. Your valuable comments are high appreciation for us.

Rajeev

Recent Posts

OWIN Authentication in .NET Core

OWIN (Open Web Interface for .NET) is an interface between web servers and web applications…

2 years ago

Serializing and Deserializing JSON using Jsonconvertor in C#

JSON (JavaScript Object Notation) is a commonly used data exchange format that facilitates data exchange…

2 years ago

What is CAP Theorem? | What is Brewer’s Theorem?

The CAP theorem is also known as Brewer's theorem. What is CAP Theorem? CAP theorem…

2 years ago

SOLID -Basic Software Design Principles

Some of the Key factors that need to consider while architecting or designing a software…

2 years ago

What is Interface Segregation Principle (ISP) in SOLID Design Principles?

The Interface Segregation Principle (ISP) is one of the SOLID principles of object-oriented design. The…

2 years ago

What is Single Responsibility Principle (SRP) in SOLID Design Priciples?

The Single Responsibility Principle (SRP), also known as the Singularity Principle, is a software design…

2 years ago