How to Make a Redirect in PHP

  302 views   3 months ago PHP

Use the PHP header() Function

You can simply use the PHP header() function to redirect a user to a different page.

The PHP code in the following example will redirect the user from the page in which it is placed to the URL http://www.example.com/another-page.php. You can also specify relative URLs.

<?php
header("Location: http://www.example.com/another-page.php");
exit();
?>

If you want to redirect the users from old page to a new page on a permanent basis then also mention HTTP response code in the header() function as shown in the following example, so that search engines transfer "page rank" from the old page to the new page.

<?php
// 301 Moved Permanently
header("Location: http://www.example.com/another-page.php", true, 301);
exit();
?>

 

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on harsukh21@gmail.com

Related Articles