How to Make a Redirect in PHP

Introduction

Redirecting a web page is a great way to send visitors to a different page than the one they originally requested. It can be used to direct visitors to a new page, or to a page with updated content. PHP is a powerful scripting language that can be used to create redirects. In this tutorial, we will discuss how to make a redirect in PHP. We will cover the different types of redirects, how to create them, and how to test them. By the end of this tutorial, you will have a better understanding of how to make a redirect in PHP.

How to Make a Redirect in PHP

1. Create a PHP file with the name of the page you want to redirect to. For example, if you want to redirect to a page called “example.php”, create a file called “example.php”.

2. Add the following code to the top of the file:

3. Replace “http://www.example.com/example.php” with the URL of the page you want to redirect to.

4. Save the file and upload it to your web server.

5. Test the redirect by visiting the page in your web browser.

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.href window.location.assign window.location.replace
Function Returns and stores the URL of the current page Replaces the current page. Loads a new document.
Usage Fastest 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). 

How to Make a Redirect in PHP

Redirecting a web page in PHP is a simple process that can be accomplished with just a few lines of code. By using the header() function, you can easily redirect a user from one page to another. This is useful for a variety of reasons, such as when you want to send a user to a different page after they have completed a form submission or when you want to redirect a user to a different page if they are not logged in.

Step 1: Create the PHP File

The first step is to create a PHP file that will contain the code for the redirect. This file should be saved in the same directory as the page you want to redirect from. For example, if you want to redirect from index.php, then the redirect file should be saved in the same directory as index.php.

Step 2: Add the Redirect Code

Once the file is created, you can add the code for the redirect. The code should look like this:

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

In this code, you will need to replace “http://www.example.com/” with the URL of the page you want to redirect to. Once you have done this, save the file.

Step 3: Add the Redirect to the Original Page

The next step is to add the redirect code to the original page. This can be done by adding the following line of code to the top of the page:

<?php include("redirect.php"); ?>

In this code, you will need to replace “redirect.php” with the name of the file you created in Step 1. Once you have done this, save the page and the redirect should now be in place.

Conclusion

Making a redirect in PHP is a simple process that can be accomplished with just a few lines of code. By using the header() function and the include() function, you can easily redirect a user from one page to another. This is useful for a variety of reasons, such as when you want to send a user to a different page after they have completed a form submission or when you want to redirect a user to a different page if they are not logged in.

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?