This article describes how to properly redirect a web page using an HTTP 301 status code and Location header. The 301 status code is used to indicate that a page has permanently moved. Multiple techniques are presented with recommendations.
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.
HTTP headers are sent for every web page. If you want to see what HTTP headers look like for a particular page, visit Rex Swain’s HTTP Viewer. For advanced users, I would recommend you download the Firefox web browser and install Chris Pederick’s Web Developer Extensions. Then, use the Information->View Response Headers function. In scripts, HTTP headers must be sent before sending any page content, including white space, or else an error will result.
-
HTTP 301 Redirect in ASP-VBScript
<%@ 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.)
HTTP 301 Redirect in PHP
<?php
// Permanent redirection
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.somacon.com/");
exit();
?>
If you set the Location header by itself, PHP automatically sets the status code to HTTP/1.1 302 Found.
Note, if you attempt to send headers after content has been sent, you will get a warning like, “Warning: Cannot modify header information - headers already sent by …”. Watch out for empty lines and spaces between PHP open and close tags. ASP ignores these, but PHP does not.
Read the rest of this entry