How do I make a redirect in PHP?

Created by markusLinked to 25.8m issues across 289 teams

tl;dr

To redirect a page in PHP, you can use the header() function. This must be sent to the browser before any HTML or text is outputted. To do this, you can use the following code:

header('Location: '.$newURL);

It is important to note that you should also use either die() or exit() after the header() function to ensure that the redirect is successful.

Both absolute and relative URLs can be used in the header() function. However, the Location header still uses the HTTP 302 redirect code, which is a temporary redirect. You should consider using either the HTTP 301 (permanent redirect) or HTTP 303 (other) codes instead.