How To Redirect Web Pages To Another Page with PHP or Javascript? – POFTUT

How To Redirect Web Pages To Another Page with PHP or Javascript?


PHP is a popular programming or scripting language used to develop web pages. One of the problems developers face is using PHP to redirect the user browser to the new page. for example, we have an old URL but we want to make the transition to the new URL we can achieve this simply by using  PHP redirect.

Simply Redirect

We will use the header function of PHP to provide a new URL to the client browser. Keep in mind that after using header function there should be no other operation that will output something or simple be sure header is the last function. In this example, we will redirect the current page to the http://www.poftut.com/newurl .

<?php 

/*Provide new Location with header function*/ 
header("Location:http://www.poftut.com/newurl") 
 
/*Be sure last output is from header function */ 
exit; 
?>

Please notice that we have used exit() function . This will make our page resource usage more effective.

Use JavaScript

JavaScript is mainly client-side scripting language. Redirect operation can be done with JavaScript on the client-side by providing a new URL to the windows object’s location property like below.

window.location = "http://www.poftut.com/newurl";

Even we can make a Javascript function to use it multiple times easily

function redirect(url){ 
 
window.location=url; 
 
}

redirect("http://www.poftut.com/newurl");

LEARN MORE  How To Use Curl with HTTPS Protocol and URLS?

5 thoughts on “How To Redirect Web Pages To Another Page with PHP or Javascript?”

  1. Hello, Neat post. There is an issue together with your site in web explorer, would check this?
    IE still is the market chief and a large section of people will miss your
    excellent writing due to this problem.

    Reply

Leave a Comment