Advanced Web Applications Redirecting a request to another URL or HTML page Download the entire Delphi 5 project for this document. At some point, you may want to redirect a user request to another URL or HTML page on your server. This is quite easy to do with the following code inside your OnAction event: Response.SendRedirect('http://www.borland.com');One question that you may ask is exactly what this does. The easiest way to find out is to look at the full headers that the server returns: HTTP/1.1 302 Object Moved Location: http://www.borland.com Server: Microsoft-IIS/5.0 Content-Type: text/html Content-Length: 145 <head><title>Document Moved</title></head> <body><h1>Object Moved</h1> This document may be found <a HREF="http://www.borland.com">here</a></body> But how did I produce this output? It is easy to "spy" on the headers returned by a webserver by simulating a GET request just like a web browser would make:
The thing that we are interested in looking at is the response code: The first part tells us what version of HTTP that the server supports (1.1 in this case). The second part is the response code, and the third part is a server defined response message for that code.HTTP/1.1 302 Object Moved To find out exactly what each response code means, and when you should give it, you can look at the HTTP RFC (Request For Comments): http://www.w3.org/Protocols/rfc2068/rfc2068 From that document, we can see that 302 means: 302 Found The requested resource resides temporarily under a different URI. Since the redirection might be altered on occasion, the client SHOULD continue to use the Request-URI for future requests. This response is only cacheable if indicated by a Cache-Control or Expires header field. ...In other words, you should use a response of 302 when you want to redirect the user to another URL, but don't want them to have to change the link that they came from. All the proper headers are set up for you when you use: Response.SendRedirect('http://www.borland.com');If you want to inform the web browser that the URL has been permanently moved, you need to send a response code 301: 301 Moved Permanently The requested resource has been assigned a new permanent URI and any future references to this resource SHOULD use one of the returned URIs. Clients with link editing capabilities ought to automatically re-link references to the Request-URI to one or more of the new references returned by the server, where possible. This response is cacheable unless indicated otherwise. The new permanent URI SHOULD be given by the Location field in the response. ...The reason for doing this is because some search engines will see a 301 response, and will update their database with the new link (since it was moved permanently, not temporarily like a 302 indicates). Now, Delphi doesn't have an automatic way of sending a 301 Response, so we have to manually do it:
That's it! You should be able to easily extend this to send any custom response code and message. More related Advanced Web Application documents: |
Last Modified: 19-JAN-00