Quick solution

To quickly redirect to another page using PHP, use this code snippet and change the destination URL to what you want:

PHP

This snippet will redirect the current page to the designated URL in the $destinationUrl variable.

Note: It is recommended to use the exit statement to prevent any other unwanted output to be sent to the client.

There are some other ways to redirect to another page using PHP, combined with HTML & JavaScript, and utilizing status codes for useful redirection cases.

Redirect to another page using PHP

Using header() function

The header() function will send an HTTP Request header to the client browser, signifying a destination URL to be redirected to. More about HTTP headers.

PHP

After a header() redirection, it is recommended to follow up with the exit function, as codes after the header() function might still be sent to the browser, causing issues.

To redirect to another page in php without header function, we can utilize HTML and JavaScript.

Using JavaScript with window.location.href

We can use PHP combined with HTML & Javascript for redirection using native window.location.href property, which will execute a redirection to the new URL:

PHP

Using HTML with meta tag

Another redirect to another page using PHP with HTML is to use the HTML meta tag. With this method, we can specify the delay time before the redirection happens:

PHP

Above are the popular methods for redirection to another page utilizing PHP, HTML & Javascript.

What status code should be used for redirection?

By default, the status code (or response code) of the header() function is 200, meaning a successful response with no errors. Sometimes in PHP applications, we want to put specific response codes for notifying the client browser when something happens.

For example, the popular response codes that can be used are:

  • 301Moved permanently: when the redirection is permanent, and the new URL replaces the old one.
  • 302Moved temporarily: when the redirection is temporary, indicates that the URL (before redirection) can be active after some time.
  • 400Bad request: when there are malformed requests from the client, such as bad syntax.
  • 403Forbidden: when an URL is forbidden.
  • 404Not found: when a resource is not to be found or didn’t exist.
  • 500Internal server error: when an error came from the server that causes failure to respond.

Using these status codes with PHP redirection can be useful, for example, with the redirected page containing the error/information message. Below are some examples of PHP redirection with status codes.

Examples of PHP redirection with status codes

Using 301 status code to notify users or web crawlers that a page has moved permanently:

PHP

Using 302 status code to notify that a page is temporarily moved, and can be accessible again later:

PHP

Using 403 status code to indicate that the page is forbidden to view:

PHP

Using 404 status code to indicate that a resource is not found:

PHP

Final thoughts

Above are the simple but in-depth ways to redirect to another page using PHP, with multiple examples and use cases. We hope this article is useful for you in your PHP development.