Monday, July 9, 2007

ASP Tips - 301 Moved Permanently - Redirecting URLs with ASP

If you are utilizing .asp (Windows Active Server Pages) as the underlying technology of your Windows IIS hosted websites, there is a simple 301 permanent redirect method that you can utilize without involving a server administrator. This 301 method allows you to quickly implement a 301 Moved Permanently status from the old-page.asp to the new-page.asp. Here is how it works...


Take the old-page.asp that is being renamed or moved and remove all of the code. Place the below code in place of the old HTML/XHMTL.

<%@ Language=VBScript %>
Response.Status="301 Moved Permanently"
Response.AddHeader "Location", ""

In the above 301 redirect example, you need to change the Location to the URI of your new-page.asp. Include the full URI path unless you are redirecting to a root level page (index.asp, default.asp, etc.). If you are setting up a 301 redirect for a root level page, keep the URI short and without the index.asp file name. For example...

"Location", ""

Once you've included the above code at the top of your old-page.asp, verify that it is returning the proper server header response using our Check Server Headers tool. You should see a recursive response (2 server responses) returned from the server header that look similar to this...

#1 Server Response:
HTTP Status Code: HTTP/1.1 301 Moved Permanently
Server: Microsoft-IIS/5.0
Date: Fri, 04 Jun 2004 03:11:15 GMT
Connection: Keep-Alive
Content-Length: 0
Content-Type: text/html
Cache-control: private

Redirect Target:

#2 Server Response:
HTTP Status Code: HTTP/1.1 200 OK
Server: Microsoft-IIS/5.0
Date: Fri, 04 Jun 2004 03:11:15 GMT
Connection: Keep-Alive
Content-Length: 14268
Content-Type: text/html
Cache-control: private

The first server response above is telling you that the referenced URI (old-page.asp) has been Moved Permanently. This is evident due to the HTTP Status Code of 301. The Moved Permanently is referred to as a Reason Phrase. The Reason Phrase can be any value although the official term for Status Code 301 is Moved Permanently. Other unofficial 301 Reason Phrases may be; Error or Permanently Moved.

The second server response is telling you that the Redirect Target (Location) is OK. This is how it should be when permanently redirecting an old-page.asp to a new-page.asp. The OK is the official Reason Phrase for Status Code 200.

In reference to spidering entities (crawlers, robots, spiders), the ASP code above and the ASP.NET code below will send a server header response of 301 Moved Permanently and, is instructing the spiders to update their index and replace the old URI (old-page.asp) with the new URI (new-page.asp). This could take several months before results are final.

ASP.NET Version

<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
Response.Status = "301 Moved Permanently";

Note: If your 301 is set up properly, you should not be able to browse to the old-page.asp (unless you have auto redirects turned off in your browser). The browser will automatically redirect you to the new-page.asp. This is of course the intended behavior.

To be on the safe side, you could always leave your old-page.asp in place and include something like this to alert visitors (who have auto redirects turned off) that the page has been moved and to update their link references...

The resource you've requested has been moved to a new location as of 2004-12-01. Please update your bookmarks and/or links to reflect the new location of this resource.

New URI Reference:

This old page will be removed on 2005-02-01 at which time we will assume our visitors have updated their links and the spidering entities have updated their indexes. At that time, this old page will return a 404 Not Found status. Thank you in advance for your understanding and continued support.

Edward Lewis, System Admin

Remember to insert your 301 Moved Permanently code above all other code on the page. For those who have auto redirects turned off, they will see your message above along with the new URI reference. Everyone else will be automatically redirected to the new URI reference.

No comments: