views:

75

answers:

3

How can I response redirect from http://domain.com to http://www.domain.com? Code, not Web.config, which doesn't seem to work for me.

+1  A: 

Read this

FosterZ
A: 

Use a 301 redirect to the correct url. Something like this;

Response.StatusCode = (int)HttpStatusCode.MovedPermanently;
Response.RedirectLocation = "http://www.domain.com";
Rob
because its the same page it keeps looping
maggie
Can you show us the code of how you implemented it now? Try adding a return null
Rob
+6  A: 

If you are hosting in IIS, then you can set up a HTTP redirect.

IIS6 Redirects

IIS7 Redirects

Information about 301 redirects

EDIT

You could add the following to your Page_Load method:

// Check if page is running under theperfectfajita.com. If not redirect ...
if (!HttpContext.Current.Request.Url.Host.Contains("localhost"))
{
    if (HttpContext.Current.Request.Url.Host.CompareTo("domain.com") != 0)
    {
        HttpContext.Current.Response.Redirect("http://www.domain.com" + Context.Request.Url.PathAndQuery);
    }
}
Ardman
this is good idea but i dont have access to iis
maggie