Doing HTTP redirection

Cool URLs don't change, except when they have to. If something changes we want to redirect clients from the old resource to the new one, HTTP redirection takes care of it and it's easy to do using Tonic.

class Redirect extends Resource
{
	function &get(&$request)
	{
		if (isset($this->location)) {
			if (isset($this->redirect)) {
				$responseCode = $this->redirect;
			} else {
				$responseCode = 301;
			}
			return new Response($responseCode, NULL, array(
				'Location' => $this->location
			));
		}
		return parent::get($request);
	}
}
lib/redirect.php

We create a resource class that given a location and a redirection status code returns a Response object that contains a location header.

class: redirect
redirect: 301
location: /somewhere/else
resources/something/old.html

We then just wire our old resource to our redirect class and include the location we want to redirect to as a property of the resource.

Back to documentation home
Created Feb 17, 2008, last modified Feb 17, 2008