Seize the opportunity to gain new skills and reshape your career!

Choose a free learning path and get valuable insights from first-rate courses

Code has been added to clipboard!

Creating PHP Redirects With the Header Function

Reading time 2 min
Published Feb 26, 2020
Updated Feb 26, 2020

TL;DR – PHP redirect refers to the process of transferring users from one page to another without their interaction with hyperlinks. To make PHP redirect to another page, you need to use the predefined header function.

The true nature of redirects and their use

There are many options for redirects, from the client-side redirects with HTML or JavaScript to server-side redirects with PHP. For more secure and smooth navigation from one page to another, developers suggest using the back-end options.

PHP redirects and redirection in general are for transferring users from one website to another. Many website owners use them during the reconstruction of their domains, or after merging several pages. Of course, redirection is necessary for some of the essential features, like integrating payment gateways.

How to create a PHP redirect?

PHP header redirect is a simple way of initiating a server-side redirect. In the following example, you can see the code for making PHP redirect to a page:

Example
<?php 
  header('Location: https://www.bitdegree.org');
  exit;
?>

However, PHP redirect is of the 302 type, meaning that it is more suitable for temporarily redirection (like site maintenance). You can fix this issue by making the redirect 301, which is for setting redirects permanently. Simply add true and 301 to the previous code example:

Example
header('Location: https://www.bitdegree.org', true, 301);

Note: 301 redirects are the most SEO-friendly. Search engines understand that the content has been moved permanently, and that not only the location has changed.

A high number of redirects can contribute to longer page loading times. PHP redirect is a server-side redirect, meaning that web browsers can handle them much better than client-side redirects with HTML or JavaScript. Therefore, to minimize the effects of redirecting, we recommend opting for the PHP header redirect.