Take control of your website’s URLs in ASP.NET with a custom HttpModule

How to set-up a custom HttpModule to enforce site-wide URL rules

In this article I'll show you how to add a custom HttpModule to your website, using my RedirectModule as a simple, quick and easy example.

Why use custom HttpModules?

When creating a web application, like this very website, I always consider implementing a custom HttpModule, because this is an ideal way for the developer to take control over the handling of HTTP requests.

For this very website I wrote a simple custom HttpModule called RedirectModule (simplified example above) which I use to enforce fundamental site-wide URL rules.

I believe that URLs on this website should always include the www suffix before the domain name. (You could test this by deleting the "www" from the start of the URL of this page right now.) Setting-up a website that works both with and without the www domain suffix is a common mistake which often leads to the same content on different URLs being indexed by search engines, and of course duplicated content can have a negative impact on your rank and position in search results.

Therefore, if you visit any URL on my website without putting "www" in the domain, you will end up at the correct URL incorporating the "www" suffix. I do this using a permanent redirect, which ensures that search engines like Bing and Google index the correct URL if they encounter an incorrect URL. Enforcing URL rules in this way is good for SEO, and helps you to control which URLs people use to link to and enter your website.

Step 1: add a custom HttpModule class to your project:

public class DomainRedirectModule : IHttpModule
{
    private static readonly string webDomain =
        ConfigurationManager
.AppSettings["WebDomain"];

    public void Init(HttpApplication context)
    {
        context.BeginRequest += context_BeginRequest;
    }
 
    public void Dispose()
    {
    }
 
    private void context_BeginRequest(object sender, EventArgs e)
    {
        var app = sender as HttpApplication;

        if (app == null)
        {
            return;
        }

        if (app.Request.Url.Host.Equals(
            webDomain,
            StringComparison.InvariantCultureIgnoreCase))
        {
            return;
        }

        string pathAndQuery = app.Request.Url.PathAndQuery;
        app.Response.RedirectLocation =
            string.Format(
                "{0}://{1}{2}",
                app.Request.Url.Scheme,
                webDomain,
                pathAndQuery);
        app.Response.StatusCode = 301;
        app.Response.End();
    }
}

Step 2: specify the domain name in your Web.Config:

Set the appropriate URL for each environment. This is the only domain that will be allowed in your web app when this sample code is implemented. E.g. In your local dev environment, set the domain to "localhost". E.g. In my production environment for this website, I obviously set the domain to "www.timacheson.com".

<appSettings>
      <!-- Domain of the website (in the environment where this Web.Config resides). -->
      <add key="WebDomain" value="www.timacheson.com"/>

Step 3: implement the new custom HttpModule in your Web.Config:

Set the type attribute as the namespace where your custom HttpModule class lives.

<httpModules>
      <!-- Custom HTTP Module -->
      <add name="DomainRedirectModule" type="YourProject.HttpModules.DomainRedirectModule" />

That's all!

Just follow the three simple steps (the bold headings above) and it's done. Easy! If you have any problems of questions, you're always welcome to ask me for help.

That's the beauty of ASP.NET. Another quick win.

Enjoy!

17 December 2009

Share the love:

Comments: 0

Add Comment

Tags:


  • Twitter
  • LinkedIn
  • Facebook
  • Windows Live / Messenger
  • Xbox Live
  • RSS
  • Email