What is HTTP redirection?

HTTP Redirection is an essential part of the http reponse request cycle. In an OBYX program it is very common to need to redirect the user agent (browser) to an alternative page or resource. OBYX makes this sort of redirection very easy as the response headers returned by OBYX to the user agent are configurable by the programmer

In order to trigger a redirect, the OBYX needs to return two headers to the user agent: Location and Code. The Location header defines the URI that the user agent should redirect to and the Code header determines what response code should be used. Generally for redirects we need either code 301 (permanent) or more usually 302 (temporary)

A Simple Redirect

Setting the response headers is straight forward. Obyx provides a shortcut to the headers through a space called 'http'. We need to set both the Location and Code http headers (*note the capital letters*). Here we will do a temporary redirect to the URI http://www.obyx.org/docs.html

<instruction note="Set the http location">
	<output space="http" value="Location" />
	<input value="http://www.obyx.org/docs.html" />
</instruction>
<instruction note="Set the http code to a temporary redirect">
	<output space="http" value="Code" />
	<input value="302" />
</instruction>
The page redirects to http://www.obyx.org/docs.html

How to Test

Testing the redirect is straight forward. Simply visit the page in your browser, you should be redirected to the docs page. However, you might want to test a little more thrroughly than this. Here you have a few options, many browsers like Firefox have plugins that let you inspect and edit the headers, Firebug being by far the best in my opinion.

If you really want to know whats happening then I suggest using curl. This command line utility is available on most linux flavours and it lets you do all sorts of geeky things. Try the following:

curl http://localhost/tests/redirecttests/redirect_1.obyx
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><title>302 Found</title></head><body><div class="subhead"><a class="redirect" href="http://www.obyx.org/docs.html">http://www.obyx.org/docs.html</a></div></body></html>

You can see here that a valid HTML document is returned with a title and a link to the redirected page. Now this is useful if your browser doesn't understand the headers, its still a page that can be understood. Note that this page source is returned by Apache not by OBYX.

So, where are the headers? Well curl doesnt show you those. To view them run curl with the -I switch.

curl -I http://localhost/tests/redirecttests/redirect_1.obyx
HTTP/1.1 302 Found
Date: Fri, 09 Oct 2009 11:11:19 GMT
Server: Apache/2.2.11 (Unix) mod_ssl/2.2.11 OpenSSL/0.9.8k
Connection: close
Location: http://www.obyx.org/docs.html
Content-Type: text/plain

You can clearly see the 302 code and the Location header here.

curl is great!

curl is a very useful utility. It lets you run raw requests and all sorts of fun stuff. Here some other useful things you can do with it:

curl -O http://www.google.co.uk/intl/en_uk/images/logo.gif
Downloads the file to disk
curl -A "my user agent" http://www.obyx.org/docs.html
Sends the request with specified user agent string
man curl
Tells you all about it!
- James
08 October 2009

Related posts:

You might also be interested in this post regarding the HTTP response object which allows you customise the response headers.


Last Modified: Thu, 15 Jan 2015