0

I am trying to redirect a page on a server root (http://www.new-url.com/default.aspx) to another page in a directory on the same domain (http://www.new-url.com/main/default.aspx) using the following .NET code:

private void Page_Load(object sender, System.EventArgs e) 
{ 
    Response.Status = "301 Moved Permanently"; 
    Response.AddHeader("Location","http://www.new-url.com/main/default.aspx"); 
}

When I do this, the redirect seems to loop on the directory:

http://www.new-url.com/main/main/main/main/main/main/main/main/main/main/main/main/main/main/...

Is it possible to do a 301 redirect, using .Net or IIS, to a directory withing the same domain? Is 301 redirect only possible to do from one domain to another? The web is on a Windows 2008 server using IIS7.

  • 1
    Assuming your code is in `~/default.aspx`, it should work fine. It looks like there is another redirect somewhere else that is fouling you up. – Jeff Oct 05 '11 at 21:47

2 Answers2

0

The best way to deal with something like this would be to use a HTTP Module, which will intercept the request prior to it reaching your page.

Once you're happy the redirect needs to happen you simply call:

Response.redirect("www.url.com");

m.edmondson
  • 30,382
  • 27
  • 123
  • 206
  • I thought response.redirect only produced a 302? – dan360 Oct 05 '11 at 21:50
  • I've just found [this](http://stackoverflow.com/questions/1723487/response-redirect-http-status-code) which seems to backup your comment - I didn't know this and I've updated my answer – m.edmondson Oct 05 '11 at 21:51
0

301 is definitely possible on same domain. You can do this in code or within IIS or even with web.config.

Your code above looks fine to me are you sure there is nothing else trying to redirect?

dan360
  • 361
  • 2
  • 16