Response.Redirect in Windows Azure

06 Oct 2009

I've recently created an application called GetGravatar that allows you to update your Twitter profile picture with your Gravatar. Once I'd set up my Windows Azure custom domain name I decided I wanted the web address for this new service site to be without "www" as seems to be all the rage (to keep the web address short). So, I looked at doing a Response.Redirect to catch any attempts to access my domain with "www" in it. I'm very sure that there are better ways of doing this but I added the following to my Site.master file:

    if (Request.Url.ToString().StartsWith("http://www."))
    {
        Response.StatusCode = (int)System.Net.HttpStatusCode.MovedPermanently;
        string responseUrl = Request.Url.ToString().Replace("http://www.", "http://");
        Response.Redirect(responseUrl, true);
    }

So, if somebody types in http://www.getgravatar.com (see it works) or anything with a "www." in the URL they will be redirected to a non-www page. However, the redirect seemed to go to the correct URL but with port 20000. I've no idea why this is so I had to add a special case in to remove the port.

    if (Request.Url.ToString().StartsWith("http://www."))
    {
        Response.StatusCode = (int)System.Net.HttpStatusCode.MovedPermanently;
        string responseUrl = Request.Url.ToString().Replace("http://www.", "http://");
        responseUrl = responseUrl.Replace(":20000", "");
        Response.Redirect(responseUrl, true);
    }

I've not read up to see if this is expected but I thought it was quite unexpected and may be of use to somebody else in the future.