Wednesday, September 21, 2005

The correct way to do a 301 redirect using ASP

Today, we're going to cover the correct way using asp script to perform a 301 perminant redirect. This type of redirection is used when the resource has moved perminantly. The most common example is when redirecting domain.com to www.domain.com, in order to avoid duplicate listings in search engines.
A 301 is very different to a 302 redirect, which should NEVER be used in this case. A 302 indicates the resource has been moved temporarily. If domain.com is redirected to www.domain.com, it will cause duplicate content issues.
When using Windows remote hosting, it can be difficult to get such redirects set up or removed, as it is necessary to go through the ISP's support team. There is a way of initiating a 301 redirect from within an ASP file. This is not the response.redirect command. response.redirect will always issue a 302 redirect. The correct sequence of commands is shown below:

<%@ Language=VBScript %>
<%
Response.Status="301 Moved Permanently"
Response.AddHeader "Location", "http://www.site.com/default.htm"
response.end
%>

When these redirects are performed, it happens at the server level, rather than at the HTML level. The browser never sees the ASP code, it is just read by the server, and the server knows to return the redirect code and location in the header.

You can check the header code that your server is returning for any of your pages using an online tool such as the Server Header Checker. It is always a good idea to check any new redirects with such a tool, and ensure that it is returning a 301 code, as accidently using the wrong one can have disasterous consequences.

No comments: