Best way to do a redirect to different URL before page load in PHP

Let’s say that you want all the visitor on the webpage to This can be done using several methods that invlove PHP, JavaScript and HTML. In this article, you will learn about all three different techniques that can be used to redirect your visitors from one webpage to another. Here are a few variables that we are going to use:


$new_url = '';

On This Page — Using the PHP header() Function to Redirect a URL, Output JavaScript Redirection Code using PHP echo() Function, Using HTML meta Tags to Redirect a Webpage and Quick Summary.

Using the PHP header() Function to Redirect a URL

If you want to redirect initial.php to final.php, you can put the following code inside the initial.php webpage. This code sends a new HTTP location header to the browser.


$new_url = '';
header('Location: '.$new_url);

Here we have use the header() function in PHP to make the redirect. The important thing that you should keep in mind is that the above code should be placed before any HTML or text output. Otherwise, you will get an headers already sent error. You can also use output buffering in order to get rid of the headers already sent error. The following example shows it in action:


$new_url = '';
header('Location: '.$new_url);

The ob_start() function should be the first function in your PHP script when you want to perform a redirect using the header() function in PHP. This will prevent the headers already sent error from occurring.

As a safety measure, you might want to add die() or exit() directly after the header redirect in order to prevent the rest of the code of the webpage from executing. Sometimes, a crawler or a browser will not respect the Location header and this might jeopardize the security of your website.


$new_url = '';
header('Location: '.$new_url);

To be clear, die() or exit() have nothing to do with the redirection. They are used to prevent the rest of the code on the webpage from executing.

It is also recommended to use absolute URLs while specifying a Location header value. However, relative URLs will also work just fine. You can also use this function to redirect users to external websites or webpages.

Output JavaScript Redirection Code using PHP echo() Function

This is not a pure PHP based solution but that doesn’t diminishes its effectiveness. You can use the PHP echo() function in order to output the JavaScript code that can handle the redirection for you.

With this solution, you will not have to use output buffering and you can also prevent the headers already sent error from occurring. If you a little JavaScript with PHP is not a problem, this could be a really clever solution. Here are a few examples that use different JavaScript methods to redirect from the current page to another.


echo "<script>self.location='';</script>";
echo "<script>document.location.href='';</script>";
echo "<script>window.location.href='';</script>";
echo "<script>window.location.replace('');</script>";

The only downside with this method is that JavaScript runs client-side and your visitors can disable JavaScript if they want to.

Using HTML meta Tags to Redirect a Webpage

You can also use basic HTML to perform a redirect from one webpage to another. This may not look very professional but it works without worrying about a client disabling the JavaScript or the headers already sent error.


<meta http-equiv="Location" content="">
<!-- The following line will redirect after the given number of seconds. Zero in our case. -->
<meta http-equiv="refresh" content="0;url=">

You can also use the last line from the previous code block in order to auto-refresh a page every “n” seconds. For example, the following line will automatically refresh the page after every 8 seconds.


<meta http-equiv="refresh" content="8">

Quick Summary

Let’s recap everything that we have covered in this tutorial.

  1. The easiest way to redirect users to a different page in PHP is to use the header() function. It is important to call this function before any HTML output in order to avoid headers already sent error.
  2. Another way to avoid the headers already sent error is to use PHP echo() function to output JavaScript code to do the redirect. The problem with this solution is that it will not work if the users have disabled JavaScript.
  3. One final method to redirect users to a different URL is to use HTML meta tags. These tags can also be used to refresh the same webpage after ‘n’ number of seconds.

Let me know if there is anything that you would like me to clarify in this tutorial. Also, you are more than welcome to comment if you know other techniques to redirect your users to a different page in PHP.

Reader Comments

1. You can register or login to post a comment. Asking readers to register improves the quality of discussion.

2. As a registered user, you will also get to access features like choosing between a light and dark theme or anything else that we might implement in future.

Follow Us For Updates

Go To Top