PHP URL Redirect code
Mark E.
In PHP, you can use the header()
function to redirect a webpage to a new URL. Here's an example of how to use the header()
function to redirect a webpage:
<?php
header("Location: https://www.newurl.com");
exit();
?>
In the above code, the header()
function specifies the location header with the URL to which the page should be redirected. The exit()
function is used to stop further execution of the script after the redirect.
You can also set the HTTP status code to indicate the type of redirect you want to perform. For example, if you want to perform a permanent redirect, you can set the HTTP status code to 301 like this:
<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: https://www.newurl.com");
exit();
?>
It is recommended to use server-side redirects like this instead of client-side redirects like the Meta Refresh tag to improve your website's SEO and user experience.