Response.Redirect vs. “301 Moved Permanently”
Posts by Colin TanJanuary 18, 2023
We’ve all used Response.Redirect at one time or another. It’s the quick and easy way to get visitors pointed in the right direction if they somehow end up in the wrong place. But did you know that Response.Redirect sends an HTTP response status code of “302 Found” when you might really want to send “301 Moved Permanently”?
The distinction seems small, but in certain cases it can actually make a big difference. For example, if you use a “301 Moved Permanently” response code, most search engines will remove the outdated link from their index and replace it with the new one. If you use “302 Found”, they’ll continue returning to the old page.
The question now becomes, how does one actually send an HTTP response status code of “301 Moved Permanently”? While it’s not as easy as doing a Response.Redirect, it’s still pretty simple. Take a look at the following two lines of code:
Response.Status = "301 Moved Permanently"
Response.AppendHeader("Location", "http://www.asp101.com/")
Using this code instead of Response.Redirect will send a “301 Moved Permanently” HTTP response status code instead of “302 Found”.
For more information on HTTP status codes, check out Wikipedia’s List of HTTP Status Codes or, if you really want to get your hands dirty go ahead and read the section on redirection from RFC 2616 – Hypertext Transfer Protocol — HTTP/1.1.
Update: ASP.NET Response.RedirectLocation
It appears that ASP.NET actually includes a Response.RedirectLocation property which you can set instead of using the AppendHeader method we originally used. The end result is the same, but based on the feedback we’ve received it seems that many of our readers prefer the RedirectLocation method.
The code is basically the same… just a slight syntax change:
Response.Status = "301 Moved Permanently"
Response.RedirectLocation = "http://www.asp101.com/"