Thursday, 13 December 2012

ASP: permanent redirect with HTTP 301 status code


There are situations when we need to redirect the visitor to a web site to another place.
This is done for example when the web site is no longer updated or valid.
In order to do so we can redirect the visitor permanently with HTTP 301 response code. That is done because we need to tell the user-agent that the location is permanently moved elsewhere.
I believe you all know what HTTP headers are... or maybe we should start from there...

HTTP headers
HTTP headers are components of the message of request and response in an HTTP transaction. Therefore they define the parameters of the transaction.
They normally have the form of pairs and examples are Cache-Control: no-cache or Content-Type:·text/html;·charset=UTF-8. If you need to see headers from a web page, you can always visit Rex Swain's HTTP Viewer submit an URl and get the headers.

Response redirect?
In ASP we can redirect to another page using Response.Redirect. However the Response.Redirect will generate a status code of 302 which means "The requested resource resides temporarily under a different URI." (quote from W3C). And that's not ok with us... we need to have a 301 response code.
In the end Response.Redirect is not a feasible solution.

The solution
We need to use something else. And the best way is to use a Response.AddHeader. Basically we need to issue the status code and then redirect the user:
Response.Status = "301 Moved Permanently"
Response.AddHeader "Location", ""
We set the status, we add the header and that's it.

I hope the above will help someone. As usual please use the comments section below for sharing your ideas and experiences.

0 thoughts:

Post a Comment

Comments are moderated. I apologize if I don't publish comments immediately.

However, I do answer to all the comments.