How to Make A PHP Redirect

How to Make A PHP Redirect

In this article we’ll be telling you how to make a PHP redirect using the header() function, which is generally for redirecting users to other pages, and a few alternatives to PHP redirect. Using the header() function, however, is a bit dangerous if certain precautions aren’t taken, that’s why we’ll also be showing you a few alternatives which are much more flexible.

Using the header() function

To do a PHP redirect, just use the following function after replacing “URL” by the URL you wish to redirect to:

header('Location: '.$URL.php);

Since you wish the redirect to happen before any HTML or PHP or any other code runs, this function should be put at the top of the page, before any kind of declaration like <!DOCTYPE>, or anything else.

Die() or exit()

We’re not done yet. Normal users will get redirected but crawlers and bots would ignore the header, making the older page accessible to them. So you can’t depend on the security offered by the header() function. One way of securing it could be using die() or exit() after the header() so that the redirect is shut down if it’s being ignored. Use the following command to do so:

header("Location: .$URL.php");
die();

The problem  with relative URLs

RFC 7231 is a great way to use absolute and relative URL redirects but it’s always good to be extra cautious while using the latter. There’s a chance that your redirects would just straight up break if you use a website builder because some collect and rename PHP pages. We’re sorry for not being able to provide you with any solution for this, there just isn’t one at this moment.

The problem with status codes

So we all know the problem with the HTTP 302 code, i.e., if PHP’s location operator returns it, many times web browsers don’t perform a real redirect and instead use the GET command, and you don’t want that to happen. But this is precisely what standard PHP redirects do. Therefore, it’s wise that you specify the code returned. Now, the options left are HTTP 301 and HTTP 303. Using HTTP 301 can be a little problematic because a permanent redirect is indicated while HTTP 303 causes indexing problems. However, HTTP 303 can be used until there’s no other solution.

Understanding what’s in the manuals

It’s really important that you know what you’re doing. We’d recommend reading the PHP manual and the W3C documentation before publishing PHP redirects. This would help you in understanding the best ways to do it. It’s also a good idea to check the list of common network vulnerabilities and protect your website against them.

How to Make A PHP Redirect

Alternatively

While PHP redirects are really fast to execute and also contribute in improving website speed, they still have their issues, as pointed out above. And if using a PHP redirect doesn’t make sense to you, here are some alternatives with the cost of slow execution.

Using the <meta> element

One way to do this is using the HTML <meta> element. The working of this is fairly simple. It’ll redirect from within the HTML portion of the page.

<meta http-equiv="refresh" content="0;url=yournewpage.php">

Using JavaScript

Another way this can be done is using JavaScript.

window.location.replace("http://example.com");

Using multiple PHP redirects

Using multiple PHP redirects can be a problem because first of all, this would affect the performance of your website. The thing is those different web hosts might not be equal and you’re sending your users on “not direct” routes. Another problem is that without your knowledge, data from your users might be getting sent to the page that you’re redirecting from. So, if you’re in a similar situation, it might be better to work on the structure of your website.

We hope we’re able to help you in making safe PHP redirects with the above information.

  • Want to get top recommendations about best hosting? Just click this link!

How to Install MySQL on a Windows Web Server Running Apache

This tutorial will show you how to install the MySQL database on a Windows serve
3 min read
Michael Levanduski
Michael Levanduski
Expert Hosting Writer & Tester

How to Install OpenCart on an Ubuntu 18.04 Server or VPS with Apache, MariaDB and PHP 7

In this how-to, you'll learn how to install OpenCart, an open source shopping ca
3 min read
Angela Olaru
Angela Olaru
Senior Writer & Hosting Expert

How to Configure PHP Versions and Options on cPanel

This guide will show new webmasters about switching to different PHP versions an
2 min read
Avi Ilinsky
Avi Ilinsky
Hosting Expert

How To Install Apache, MySQL & PHP on an Ubuntu 18.04 VPS or Dedicated Server

This article shows users how to setup PHP, Apache web server and MySQL database
4 min read
Idan Cohen
Idan Cohen
Marketing Expert
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.
Click to go to the top of the page
Go To Top