Forward One Page to Another Page or URL in PHP

In order to redirect or forward a certain page to another page or url we can use the PHP header() function.

For example if we place the following code on index page of lookouhost.com then it will redirect to Google.com upon opening Lookouhost.com

<?php
header("Location: https://google.com/");
exit();
?>

Same can be done within the website. For instance if a user opens a lookouhost.com/aboutus.php we can place following code to automatically redirect him to page lookouthost.com/contactus.php

<?php
header("Location: https://lookouhost.com/contactus.php");
exit();
?>

This can be usefull when used in combination with other functions like redirect a user on the basis of their origin, ip or any other criteria which you can define with php. This way you can show a visitor meeting certain criteria a different page. You can lookout our guides on how to get user ip in php or how to get country name, currency etc in php here. 

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: https://lookouhost.com/newcontactus.php", true, 301);
exit();
?>




Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments