Pages

Wednesday, July 21, 2010

Generating and enforcing that any link requested is converted to lowercase Using ASP.NET(C#)



Generating and enforcing that any
link requested is converted to lowercase Using ASP.NET(C#)



Asp.Net Version: All Asp.net Version
supports this method.



Some search
engines interpret URLs with different case sensitivity differently for e.g. “http://www.example.com/Home.aspx”
and “http://www.example.com/home.aspx” as two different locations.



So now the
problem arises how we can enforce that all my requested URLs get rendered as
lowercase, and how do I force a request to a URL with an uppercase letter to
redirect to the lowercase equivalent so that Search engine stops treating URLs
having different cases differently.



To ensure
that any request to the site is enforced to be lowercased, and perform a 301
redirect if not I have created an HttpModule, and named it EnforceLowecaseRequestHttpModule.



To integrate
this we can either register it in the web.config file or we could directly put
this into the global.asax file.



I will
suggest creating an HttpModule and integrate it in web.config file to make it
easily reusable on future projects.



Let’s follow
step by step procedure to create HttpModule and get register in Web.Config
file.



Step 1.
First create a file (EnforceLowercaseRequestHttpModule.cs) in App_code folder
in you applications and paste the following code:



using System;



using System.Data;



using System.Configuration;



using System.Web;



using System.Web.Security;



using System.Web.UI;



using System.Text;



using System.Text.RegularExpressions;



public
class EnforceLowercaseRequestHttpModule : IHttpModule



{



public void Dispose()



{



}



//Call
Init functionto register function



public
void Init(HttpApplication context)



{



context.BeginRequest +=
context_BeginRequest;



}





//Declare
function where requested URL is checked for any UpperCase and if //found
convert it to LowerCase





private
void context_BeginRequest(object sender, EventArgs e)



{



HttpApplication application =
(HttpApplication)sender;





//
If upper case letters are found in the URL then convert and //redirect to lower case URL





string
requestedUrl = ((application.Context.Request.Url.Scheme + "://" +
application.Context.Request.Url.Authority +
application.Context.Request.Url.AbsolutePath));





if
(Regex.IsMatch(requestedUrl, @"[A-Z]") == true)



{



string
lowercaseUrl = requestedUrl.ToLower();



lowercaseUrl
+= HttpContext.Current.Request.Url.Query;





application.Context.Response.Clear();



application.Context.Response.Status
= "301 Moved Permanently";



application.Context.Response.AddHeader("Location",
lowercaseUrl);



application.Context.Response.End();



}



}



}



Step 2. Next
step is to register this HttpModule in the project and to do this we will
follow simple steps:



Open
Web.config file and paste the following code in “<httpModules>” tag as
follows.



To register the module for IIS 6.0
and IIS 7.0 running in Classic mode



<configuration>




<system.web>




<httpModules>



<add name="EnforceLowercaseRequestHttpModule" type="EnforceLowercaseRequestHttpModule"/>




</httpModules>




</system.web>



</configuration>



Registering the HTTP Module in IIS
7.0 Integrated Mode



<configuration>




<system.webServer>




<modules>



<add name="EnforceLowercaseRequestHttpModule" type="EnforceLowercaseRequestHttpModule"/>




</modules>




</system.webServer>



</configuration>



That’s all
and now this can easily handle URLs having UpperCase characters to lower case.



Happy
coding………………………………….



2 comments: