How to Make a Redirect in PHP

February 9, 2023

Introduction

Redirects are particularly useful when web admins are migrating websites to a different domain, implementing HTTPS, or deprecating pages.

There are several ways to implement a redirect, with PHP redirects being one of the easiest and safest methods. A PHP redirect is a server-side redirect that quickly redirects users from one web page to another.

Even though PHP redirects are considered safe, not implementing them correctly can cause serious server issues.

This guide explains two ways to set up a PHP redirect, and the advantages and disadvantages of each method. 

How to set up a PHP redirect.

Prerequisites

Method 1: PHP Header Function

The fastest and most common way to redirect one URL to another is by using the PHP header() function.  

The general syntax for the header() function is as follows: 

header( $header, $replace, $http_response_code ) 
  • $header. The URL or file name of the resource being redirected to. Supported file types include but are not limited to HTML, PDF, PHP, Python, Perl, etc.
  • $replace (optional). Indicates whether the current header should replace a previous one or just add a second header. By default, this is set to true. Using false forces the function to use multiple same-type headers.
  • $http_response_code (optional). Sets the HTTP response code to the specified value. If not specified, the header returns a 302 code.

Important: To correctly redirect using the header() function, it must be placed in the page's source code before any HTML. Place it at the very top of the page, before the !DOCTYPE declaration.

Consider the following PHP code example:

<html>
<?php  
header("Location: http://www.example.com/example-url", true, 301);  
exit();  
?> 

The defined header() function redirects the page to http://www.example.com/example-url, replaces any previous header function and generates a 301 response code.

The exit() or die() function is mandatory. If not used, the script may cause issues.

Note: Permanent (301) redirects are typically used for broken or removed pages. That way, users are redirected to a page relevant to them.

Method 2: JavaScript via PHP 

If using the PHP header() function is not a viable solution, use JavaScript to set up a PHP redirect. Take into consideration that JavaScript redirects via PHP work slower and require the end user’s browser to have JS enabled and downloaded. 

There are three ways to set up a PHP redirect using the JS window.location function: 

  • window.location.href
  • window.location.assign
  • window.location.replace

Below is a brief overview of the differences between the three options:

window.location.hrefwindow.location.assignwindow.location.replace
FunctionReturns and stores the URL of the current pageReplaces the current page.Loads a new document.
UsageFastest JS redirect method.Used when the original webpage needs to be removed from browser history.Safer than href.
Does it show the current page?
Does it add a new entry to the user’s browser history?
Does it delete the original page from the session history?

window.location.href 

window.location.href sets the location property of a page to a new URL.  

The following code is used to call window.location.href via PHP. 

<?php 
<script type="text/javascript"> 
window.location.href="http://www.example-url.com" 
</script> 
?> 

The advantage of window.location.href is that it is the fastest-performing JS redirect. The disadvantage is that if the user navigates back, they return to the redirected page.

window.location.assign 

window.location.assign() calls a function to display the resource located at the specified URL. 

Note: window.location.assign() only works via HTTPS. If the function is used for an HTTP domain, the redirect does not function, and it displays a security error message instead.

The following code snippet calls the window.location.assign() via PHP: 

<?php 
<script type="text/javascript"> 
window.location.assign("http://www.example-url.com")
</script> 
?> 

The disadvantage of using window.location.assign() is that its performance and speed is determined by the browser's JavaScript engine implementation. However, its the safer option. Should the target link be broken or unsecure, the function will display a DOMException – an error message. 

window.location.replace 

window.location.replace() replaces the current page with the specified URL. 

Note: window.location.replace() only works on secure domains (HTTPS) too.

The following code is used to call window.location.replace() via PHP: 

<?php 
<script type="text/javascript"> 
window.location.replace("http://www.example-url.com")
</script> 
?> 

Once a page is replaced, the original resource does not exist in the browser's history anymore, meaning the user cannot click back to view the redirected page.

The advantage of window.location.replace() is that, just like window.location.assign(), it does not allow redirects to broken or unsecure links, in which case it outputs a DOMException .

The disadvantage of window.location.replace() is that it may perform slower than window.location.href()

PHP Header Vs. JS Methods - What to Use? 

The general consensus is that the PHP header() function is the easiest and fastest way to set up a PHP redirect. JavaScript via PHP is typically reserved as an alternative when setting up the PHP header fails.

This is due to two reasons: 

  • JavaScript must be enabled and downloaded on the end user’s browser for redirects to function. 
  • JavaScript redirects perform slower. 

Conclusion

You now know how to set up a PHP redirect using the header() function or through some clever use of JavaScript.

After setting up redirects, monitor their performance. A routine website audit can detect the presence of redirect loops and chains, missing redirects or canonicals, and potential setup errors (redirects being temporary instead of permanent, and vice versa). 

Was this article helpful?
YesNo
Mirjana Fodora
Mirjana Fodora is a Technical Writer with a background in Web Design and Development. Despite being one of the youngest members on the team, her writing skills and technical aptitude help her produce factual, informative, and user-friendly content.
Next you should read
How to Troubleshoot ERR_TOO_MANY_REDIRECTS
March 3, 2022

The ERR_TOO_MANY_REDIRECTS error occurs when the browser is redirected to a different URL, which in turn...
Read more
4 Different Types of Errors in PHP
August 6, 2019

A PHP Error occurs when something is wrong in the PHP code. The error can be as simple as a missing semicolon, or as complex as...
Read more
How to Install phpMyAdmin on CentOS 8
June 2, 2020

PhpMyAdmin is a graphical utility for managing databases. It’s typically used to remotely manage MySQL or MariaDB databases.
Read more
How To Install PHP On Ubuntu 20.04 or 22.04
November 24, 2022

Read our tutorial on how to install PHP on Ubuntu 20.04 or 22.04 including integration with Apache and Nginx.
Read more