posted on March 03, 2011 05:30
In order to redirect an out-of-print web page to another location, return the HTTP 301 status code and a location header in the HTTP response of the deprecated web page. The HTTP 301 response code will tell user-agents that the location has permanently moved. This is particularly useful for search engines like Google, which will carry over page rank to the new page if this status code is seen. If you do not need to indicate permanent displacement, you can accomplish redirection by setting a Location header in PHP or using Response.Redirect in ASP. The location header does the actual redirection to the new location, and can be used by itself.
<%@ Language=VBScript %>
<%
' Permanent redirection
Response.Status = "301 Moved Permanently"
Response.AddHeader "Location", "http://www.somacon.com/"
Response.End
%>
In Active Server Pages (ASP), Response.Redirect
does not work the same as the code shown in the example. Response.Redirect
will set the location header as shown, but it will set the status code to HTTP/1.1 302 Object moved instead. When you set the Location header with Response.AddHeader
, the status code must be manually defined, otherwise it stays 200 OK.
If you send any page content prior to the headers, you will get an error like, "Response object error 'ASP 0156 : 80004005'; Header Error; The HTTP headers are already written to the client browser. Any HTTP header modifications must be made before writing page content.". Normally, you do not see this error even if there is content prior to the redirect, because page buffering is enabled by default in IIS. If you want to be sure there is no content being sent before the redirect, call Response.Flush
just before it, disable page buffering with Response.Buffer = False
, or configure IIS to disable page buffering. (Disabling buffering reduces performance.)