tags:

views:

423

answers:

4

I am using asp.net 3.5 and IIS 6.

How can we automatically redirect pages from http(s)://example.com/* to http(s)://www.example.com/* ?

thanks.

A: 

I think that's best done with DNS.

tooshel
Without knowing why the poster wants to do this, it is unlikely that DNS is a suitable approach IMHO. Firstly, the only way I can think of doing it in DNS is with a CNAME which is not a desperately good approach. Secondly, it is highly likely that the poster specifically wants to do the redirection in IIS or in code, given the way the question is tagged.
Rob Levine
@Rob - agreed - generally, the requirement is to have all possible host names resolve to one to ensure that search engines only list one instance of your site - as the OP is asking "how can we redirect doming.com to www.domain.com", I would assume that the DNS is already configured, and both versions are serving the same site.
Zhaph - Ben Duguid
A: 

This MSDN page might help you.

Chris Arnold
+5  A: 

I did this with an HttpModule:

namespace MySite.Classes
{
  public class SeoModule : IHttpModule
  {
    // As this is defined in DEV and Production, I store the host domain in
    // the web.config: <add key="HostDomain" value="www.example.com" />
    private readonly string m_Domain =
                            WebConfigurationManager.AppSettings["HostDomain"];

    #region IHttpModule Members

    public void Dispose()
    {
      //clean-up code here.
    }

    public void Init(HttpApplication context)
    {
      // We want this fire as every request starts.
      context.BeginRequest += OnBeginRequest;
    }

    #endregion

    private void OnBeginRequest(object source, EventArgs e)
    {
      var application = (HttpApplication) source;
      HttpContext context = application.Context;

      string host = context.Request.Url.Host;
      if (!string.IsNullOrEmpty(m_Domain))
      {
        if (host != m_Domain)
        {
          // This will honour ports, SSL, querystrings, etc
          string newUrl = 
               context.Request.Url.AbsoluteUri.Replace(host, m_Domain);

          // We would prefer a permanent redirect, so need to generate
          // the headers ourselves. Note that ASP.NET 4.0 will introduce
          // Response.PermanentRedirect
          context.Response.StatusCode = 301;
          context.Response.StatusDescription = "Moved Permanently";
          context.Response.RedirectLocation = newUrl;
          context.Response.End();
        }
      }
    }
  }
}

Then we need to add the module to our Web.Config:

Find the section <httpModules> in the <system.web> section, it may well have a couple of other entries in there already, and add something like:

<add name="SeoModule" type="MySite.Classes.SeoModule, MySite" />

You can see this in action here:

All end up on http://www.doodle.co.uk

Zhaph - Ben Duguid
thank you. it works perfectly
Anwar Chandra
No problem (why is saying thanks so hard? oh, because I have to type an essay in the comments box. I remember when this box took less than 200 characters. Look at it now, demanding lots of characters, and accepting 600 in total - it's bloat and scope creap I tell you!)
Zhaph - Ben Duguid
Very good! One thing that didn't work well for me though was if I had Url Rewriting in place. I found changing the newUrl string to the following (in VB) fixed the problem though: **Dim newUrl As String = String.Format("http{0}://{1}", IIf(CBool(context.Request.ServerVariables("HTTPS") = "off"), "", "s"), m_Domain + context.Request.RawUrl)**
Curt
A: 

In general, the performance will be better if you let IIS handle the redirection. To do that, create a new web site with the host header set to example.com, and use IIS Manager to configure the redirection.

RickNZ
The problem with this approach is that there's no way to redirect from `https://domain.com` to `https://www.domain.com` - the redirect options for "another website or directory" don't allow for a wildcard on the secure portion (http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/6b855a7a-0884-4508-ba95-079f38c77017.mspx?mfr=true).
Zhaph - Ben Duguid