Redirecting in PHP is easy:
$url = "http://coding-journal.com/"; 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:
$url = "someRelativeUrl/"; t3lib_utility_http::redirect($url);
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.
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_http_utility, which have the following format:
$status = t3lib_http_utlity::HTTP_STATUS_XXX;
XXX may be any valid HTTP status code, such as
Happy redirecting :)