Proper redirecting in TYPO3 (from within PHP)

Redirecting in PHP is easy:

$url = "";
header("Location: $url");

But there is tons of stuff to worry about when doing a header redirect, for instance:

  • What HTTP status will be sent?
  • Is the URL I’m throwing into the header-call really absolute?
  • Does my script exit after the header-call? (Because there should be no HTTP body if you send a Location: ... header and sending one may lead to unexpected behavior in some browsers).

Today I found a neat little method that does all the worrying for you, at least when you are using TYPO3: t3lib_utility_http::redirect:

$url = "someRelativeUrl/";

This will happily redirect you to that URL relative to your current TYPO3 root (or your baseURL if you’ve specified one). It will also send a 303 See Other Header by default, and call exit() after the redirect, making sure not additional content is sent.

The redirect-method of course also works if you supply an absolute URL. You may also pass different HTTP statūs using the second parameter and the constants defined in t3lib_utility_http, which have the following format:

$status = t3lib_utility_http::HTTP_STATUS_XXX;

Where XXX may be any valid HTTP status code, such as 200 or 404.

Happy redirecting :)

One thought on “Proper redirecting in TYPO3 (from within PHP)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>