welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Tuesday, January 14, 2014

How to Redirect to non-www-url to www domain with ASP.NET?

you have to update to this setting in web.config file........

public class SeoUrls : IHttpModule
  #region IHttpModule Members

  public void Init(HttpApplication context)
      context.PreRequestHandlerExecute += OnPreRequestHandlerExecute;

  public void Dispose()


  private void OnPreRequestHandlerExecute(object sender, EventArgs e)
    HttpContext ctx = ((HttpApplication) sender).Context;
    IHttpHandler handler = ctx.Handler;

    // Only worry about redirecting pages at this point
    // static files might be coming from a different domain
    if (handler is Page)
      if (Ctx.Request.Url.Host != WebConfigurationManager.AppSettings["FullHost"])
        UriBuilder uri = new UriBuilder(ctx.Request.Url);

        uri.Host = WebConfigurationManager.AppSettings["FullHost"];

        // Perform a permanent redirect - I've generally implemented this as an 
        // extension method so I can use Response.PermanentRedirect(uri)
        // but expanded here for obviousness:
        response.AddHeader("Location", uri);
        response.StatusCode = 301;
        response.StatusDescription = "Moved Permanently";
Then register the class in your web.config:
  <add type="[Namespace.]SeoUrls, [AssemblyName], [Version=x.x.x.x, Culture=neutral, PublicKeyToken=933d439bb833333a]" name="SeoUrls"/>