To create a redirect in PHP, you can use the ‘header()
‘ function. This function allows you to send a raw HTTP header to the client. To create a redirect, you need to send a ‘Location
‘ header with the URL of the page you want to redirect the user to. Here is an example of how to create a redirect:
<?php
// Redirect to the homepage
header("Location: /index.php");
exit;
?>
This will redirect the user to the homepage of the website. You can also use the ‘header()
‘ function to specify the HTTP status code for the redirect. For example, to create a 301 permanent redirect, you can do the following:
<?php
// Redirect to the homepage with a 301 status code
header("HTTP/1.1 301 Moved Permanently");
header("Location: /index.php");
exit;
?>
Note that the ‘header()
‘ the function must be called before any output is sent to the browser, so make sure to call it before any HTML or other content is printed.