×
PHP Redirects – How to Create and Configure them

PHP redirect is a method used to redirect a user from one page to another page without clicking any hyperlinks.

This will be helpful in such circumstances when you want to redirect a certain page to a new location, change the URL structure of a site and redirect users to another website.

Redirection is very important and frequently used in Web Development phases.

There are several reasons to use PHP redirect, including, Merger of two websites, Change of business name, Redirect traffic to updated content and many more.

In this tutorial, we will learn how to redirect PHP page with the header() function.

PHP Redirect Basic Syntax

To use a redirect in PHP, we use a header() function. The header() function is an inbuilt function in PHP which is used to send a raw HTTP header to the client.

Basic syntax of header() function in PHP redirect is shown below:

header( header, replace, http_response_code )

Or

< ?php header("Location: http://www.redirect.to.url.com/");
exit;
?>

Each parameter is described below:

  • header:
    This is used to hold the header string to send
  • replace:
    Indicates the header should replace a previous similar header, or add a second header of the same type
  • http_response_code:
    This is used to hold the HTTP response code

PHP Redirect Example

In this section, we will give you a quick example of how to create a redirect using PHP.

In this example, we will create a page1.php that contains code that issues a redirect and page2.php that contains just HTML.

Let’s create a page1.php:

nano /var/www/html/page1.php

Add the following contents:

<?php
header("Location: page2.php");
exit;
?>

Save and close the file. Then, create a page2.php:

nano page2.php

Add the following contents:

<html>
<head>
<h1>This my page2</h1>
</head>
</html>

Save and close the file, when you are finished.

Next, you can test the URL redirection by visiting the page1.php at URL http://localhost/page1.php. You will be redirected to the page2.php as shown below:

php-redirect

You can also test the URL redirection with Curl command:

curl -I http://localhost/page1.php

You should see the following output:

HTTP/1.1 302 Moved Temporarily
Server: nginx/1.4.6 (Ubuntu)
Date: Wed, 11 Sep 2019 09:48:42 GMT
Content-Type: text/html
Connection: keep-alive
X-Powered-By: PHP/5.5.9-1ubuntu4.29
Location: page2.php

By default, search engine replies with the default response code 302 while Browser reply with the response code 30x.

If you want to redirect page1.php to another site https://www.webservertalk.com with response code 301 then edit the php1.php file with the following contents:

nano page1.php

Add the following contents:

<?php
header("Location: https://www.webservertalk.com",TRUE,301);
exit;
?>

Save and close the file. Then, check PHP redirect again with the Curl command:

curl -I http://localhost/page1.php

You should see the following output:

HTTP/1.1 301 Moved Permanently
Server: nginx/1.4.6 (Ubuntu)
Date: Wed, 11 Sep 2019 10:21:58 GMT
Content-Type: text/html
Connection: keep-alive
X-Powered-By: PHP/5.5.9-1ubuntu4.29
Location: https://www.webservertalk.com

PHP Redirect with Absolute URL

In the above examples, The URL does not contain a hostname, this will work on modern browser. But, it is better to redirect to an absolute URL.

You can achieve this by editing the page1.php file as shown below:

nano page1.php

Make the following chages:

<?php
header('Location: http://' . $_SERVER['HTTP_HOST'] . '/page2.php');
exit;
?>

Save and close the file when you are finished. Then, you can test it with your web browser or Curl command.

PHP Redirect with Time Delay

You can also redirect PHP page to another page with refresh function instead of Location.

For example, create a page1.php that redirect to page2.php after 10 seconds:

nano page1.php

Add the following contents:

<?php
header( "refresh:10; url=/page2.php" );
echo "Redirecting in 10 secs.";
exit;
?>

Save and close the file. Then, check the URL redirection by visiting the URL http://localhost/page1.php. You should see the following page:

Above page indicates that page1.php will redirects after 10 seconds.

Conclusion

In the above tutorial, we have learned how to redirect URL from one page to another with PHP header() function.

I hope you have now enough knowledge on how PHP redirection works. For more information, you can visit the PHP redirect documentation at PHP redirect.