Wednesday, February 07, 2007
Wednesday, February 07, 2007 3:44:13 AM (GMT Standard Time, UTC+00:00) (ASP.Net)

Having recently moved my DasBlog tech blog from one domain to another I wanted an easy way to redirect all the traffic from the old domain, to the new one.

My shared hosting provider doesn’t provide directory level redirects (although this can be done in IIS) – only domain level.

There are also plenty of examples out there for how to redirect a single page – typically Default.aspx. What I wanted was true SEO friendly Url for Url permanent HTTP 301 redirection for every page and service in the blog.

Since the blogging engine and directory contents were the same all I needed to replace was the name portion of the domain name for the incoming Urls, redirecting them to the same resource, but on the new domain.

So I knocked to together an HTTP Handler that looks like this.

namespace DotBits.Handlers
{
   public class RedirectHandler : IHttpHandler
  
{
      public void ProcessRequest(HttpContext context)
     {
        context.Response.Status = "301 Moved Permanently";
        string url = context.Request.Url.ToString().Replace("dotbits", "58bits");
        context.Response.AddHeader("Location", url);
      }

      public bool IsReusable
     {
        get { return false; }
     } 
   }
}

And then a reference to the handler with the correct path statement in the Web.config file.

<system.web>
   <
httpHandlers>
     <
add verb="*" path="blog/*.*" type="DotBits.Handlers.RedirectHandler"/>
   </
httpHandlers>
</system.web
>

Presto. Works for all registered ASP.Net resource types; .aspx, .asmx, .axd etc.

OpenID
Please login with either your OpenID above, or your details below.
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, b, em, i, strike, strong, u) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Live Comment Preview

search

categories

on this page

ads

archive

Total Posts: 97
This Year: 3
This Month: 0
This Week: 0
Comments: 92