2

I need to permanent redirect some pages, and redirect the user to the new URL as well.

This code only sets the correct headers. The user are not redirected.

public static void PermanentRedirect(this HttpResponse response, string newUrl)
{
  response.Status = "301 Moved Permanently";
  response.StatusCode = 301;
  response.AddHeader("Location", newUrl);
}

If I put:

Response.Redirect(newUrl);

at the end, a 302 Temporary Redirect is performed.

How can I 301 redirect the user?

Related Questions:

How do I programatically 301 redirect in an asp page

Community
  • 1
  • 1
MartinHN
  • 19,542
  • 19
  • 89
  • 131
  • I just wrote a blog post on that: http://www.ko-sw.com/Blog/post/Permanent-Redirect-Using-ASPNET.aspx – Kerido Feb 22 '10 at 21:19

3 Answers3

4

Try Response.Flush and Response.End. Redirect says to end the request by sending a 302.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
2

Note in ASP.NET 4.0 this is now built-in so you can use the RedirectPermanent() method. e.g.

RedirectPermanent("/newpath/foroldcontent.aspx"); 
Dan Diplo
  • 25,076
  • 4
  • 67
  • 89
0

or maybe try ISAPI? it mimicks mod_rewrite and other .htaccess functionality on IIS.

Nona Urbiz
  • 4,873
  • 16
  • 57
  • 84