How Can I Do a Redirect to Different URL Before Page Load?

Author — Nitish Kumar

Let’s say that you want all the visitor on the webpage https://example.com/initial.php to https://example.com/final.php. This can be done using several methods that involve 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:

PHP

$new_url = 'https://example.com/final.php';
On This Page

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.

PHP

$new_url = 'https://example.com/final.php';
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:

PHP

ob_start();
$new_url = 'https://example.com/final.php';
header('Location: '.$new_url);
ob_end_flush();

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.

PHP

$new_url = 'https://example.com/final.php';
header('Location: '.$new_url);
exit();

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.

PHP

echo "<script>self.location='https://example.com/final.php';</script>";
echo "<script>document.location.href='https://example.com/final.php';</script>";
echo "<script>window.location.href='https://example.com/final.php';</script>";
echo "<script>window.location.replace('https://example.com/final.php');</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.

HTML

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

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.

HTML

<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.

Rate this post —

Very PoorPoorAverageGoodExcellent (No Ratings Yet)
Loading...
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Akash
Akash
3 years ago

Thanks for sharing this article for php header. I had issue before while I used header function to redirect to another page but it does not work properly. It was html content rendered before header function. This article help for identifying issue and fixed. Thanks!

0%