Summary of existing answers plus my own two cents:
1. Basic answer
You can use the
header() function to send a new HTTP header, but this must be sent to the browser before any HTML or text (so before the
<!DOCTYPE ...> declaration, for example).
2. Important details
die() or exit()
Why you should use
exit() : The Daily WTF
Absolute or relative URL
Since June 2014 both absolute and relative URLs can be used. See RFC 7231 which had replaced the old RFC 2616, where only absolute URLs were allowed.
PHP’s “Location”-header still uses the HTTP 302-redirect code, but this is not the one you should use. You should consider either 301 (permanent redirect) or 303 (other).
Note: W3C mentions that the 303-header is incompatible with "many pre-HTTP/1.1 user agents. Currently used browsers are all HTTP/1.1 user agents. This is not true for many other user agents like spiders and robots.
HTTP Headers and the
header() function in PHP
You may use the alternative method of
http_redirect($url); which needs the PECL package peclto be installed.
5. Helper Functions
This function doesn’t incorporate the 303 status code:
function Redirect($url, $permanent = false)
header('Location: ' . $url, true, $permanent ? 301 : 302);
This is more flexible:
function redirect($url, $statusCode = 303)
header('Location: ' . $url, true, $statusCode);
header() redirects only work before anything is written out. They usually fail if invoked inmidst HTML output. Then you might use a HTML header workaround (not very professional!) like:
<meta http-equiv="refresh" content="0;url=finalpage.html">