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.

Tutorial
Introduction
Installation
Syntax
Variable
Superglobals
Data Types
String
Array
Multidimensional Array
Sort Array
Constant
Operators
Cookies
Sessions
DateTime
Error Handling
Exception Handling
File
Write and Create File
File Open, Read and Close
File Upload
Filtering
Redirecting
Advanced Filters
Forms
Form Required Field
Validate Email/URL
Form Validation
Form Action
Function
Prepared Statements
JSON
Calendar
ZIP File
FTP
HTTP Response
DateTime Functions
Error Functions
File Function
Filter
Math Functions
Mail Function
Miscellaneous Functions
Date Format
String Functions
Array Functions
Directory Functions
MySQL Database
MySQL Connection
MySQL Create Database
MySQL Create Table
MySQL Delete Data
MySQL Insert Data
MySQL Get Last Record ID
MySQL Insert Multiple Records
MySQL Select Data
MySQL Limit Data
MySQL Update Data
MySQLi Functions
AJAX and MySQL
AJAX Search
AJAX Poll
RSS Reader
Read XML File in PHP
XML Parser
SimpleXML Parser
SimpleXML: Node and Attribute
Expat XML Parser
DOMDocument
Libxml Functions
SimpleXML Functions
XML Parsing Functions
PHP isset
PHP echo and print
PHP if else and elseif
PHP switch case
PHP include File
PHP while Loop
PHP for and foreach
PHP mail()
PHP explode()
PHP substr()
PHP str_replace()
PHP array_push
PHP count()