How to Make a Redirect in PHP
Posted on
TABLE OF CONTENTS
Redirects can be handy when web admins need to migrate websites to a different domain, implement HTTPS, or deprecate pages.
There are multiple ways to implement a redirect, and PHP redirects are considered one of the simplest and most secure methods. A PHP redirect is a server-side redirect that efficiently redirects users from one web page to another.
While PHP redirects are generally considered secure, improper implementation can lead to significant server problems.
This guide provides detailed information on two methods for setting up a PHP redirect. It also discusses the pros and cons associated with each approach.
Requirements
- PHP version 4.0 (or newer) is installed.
- A text editor or IDE for editing files.
One approach is to use the PHP Header function.
One popular method for redirecting URLs is to use the PHP header() function, which is both efficient and widely used.
Here is the general syntax for the header() function:
header( $header, $replace, $http_response_code )
$header. The URL or file name of the resource being redirected to. We accept a wide range of file types, such as HTML, PDF, PHP, Python, Perl, and more.
$replace (optional). Determines if the current header should replace a previous one or add a second one. By default, this is enabled. By involving the “false” parameter, the function can be instructed to employ multiple headers of the same type.
The $http_response_code parameter is optional. Specifies the desired HTTP response code. If not specified, the header will return a 302 code.
The header() function must be placed in the page’s source code before any HTML to redirect correctly. Put it at the top of the page, right before the!DOCTYPE declaration.
Here is a PHP code example for your consideration:
<html>
<?php
header("Location: http://www.example.com/example-url", true, 301);
exit();
?>
Using the exit() or die() function is essential. Failure to utilise the script could potentially lead to complications.
Just a quick note: Permanent (301) redirects are usually employed for broken or removed pages. Users are directed to a page that is relevant to them.
Approach 2: JavaScript through PHP
If the PHP header() function is unsuitable for your needs, you can use JavaScript to establish a PHP redirect. It’s worth noting that JavaScript redirects through PHP tend to perform slower and rely on the end user’s browser having JavaScript enabled and downloaded.
There are three methods to establish a PHP redirect using the JS window.location function:
- window.location.href
- window.location.assign
- window.location.replace
Here’s a quick rundown of the distinctions among the three choices:
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 web page 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
The window.location.href method allows you to update the URL of a page.
This code can be used to call window.location.href using PHP.
<?php
<script type="text/javascript">
window.location.href="http://www.example-url.com"
</script>
?>
One of the benefits of using window.location.href is its exceptional speed as a JavaScript redirect. One drawback is that when the user returns, they are directed back to the page from which they were redirected.
window.location.assign
The function window.location.assign() displays the resource found at the given URL.
Please note that window.location.assign() is limited to HTTPS only. When the function is used for an HTTP domain, the redirect fails and instead presents a security error message.
Here is a code snippet that uses PHP to call the window.location.assign() function:
<?php
<script type="text/javascript">
window.location.assign("http://www.example-url.com")
</script>
?>
One drawback of employing window.location.assign() is that its performance and speed rely on the browser’s specific JavaScript engine. However, it is the safer choice. If the target link is broken or unsecured, the function will display a DOMException, an error message.
window.location.replace
The function window.location.replace() is used to replace the current page with a new URL.
Please note that the window.location.replace() method is only functional on secure domains (HTTPS) as well.
Here is a code snippet that can be used to call window.location.replace() using PHP:
<?php
<script type="text/javascript">
window.location.replace("http://www.example-url.com")
</script>
?>
After a page is replaced, the original resource is no longer available in the browser’s history. This means that the user is unable to navigate back to the redirected page.
One benefit of using window.location.replace() is that, similar to window.location.assign(), it prevents redirects to broken or unsecure links. In such cases, it will throw a DOMException.
One drawback of using window.location.replace() is that it might have slower performance compared to window.location.href().
PHP Header Vs. JS Methods – Which One Should You Use?
Most experts agree that the PHP header() function is the simplest and most efficient method for establishing a PHP redirect. Using JavaScript through PHP is often considered as a backup option if the PHP header setup fails.
There are two reasons for this:
- In order for redirects to work, it is necessary for JavaScript to be enabled and downloaded on the user’s browser.
- JavaScript redirects have slower performance.
In summary
PHP redirects are an effective and secure method for redirecting users from one web page to another. This guide has provided a comprehensive overview of two methods for implementing PHP redirects: using the PHP Header function and JavaScript through PHP. Each method has its own advantages and disadvantages, and the choice between them depends on the specific requirements of the project.
Now you have the knowledge to configure a PHP redirect using the header() function or by employing clever JavaScript techniques.
Once you have established redirects, it is important to monitor their performance closely. A routine website audit can help identify common issues such as redirect loops and chains, missing redirects or canonicals, and potential setup errors like using temporary redirects instead of permanent ones and vice versa.
FAQs
A PHP redirect is a technique used to automatically redirect users from one web page to another. It is useful in situations such as website migrations, HTTPS implementation, or page deprecation.
The two methods for implementing PHP redirects are the PHP Header function and JavaScript through PHP.
The PHP Header function is a widely used method for redirecting URLs. The general syntax for the header() function is header(header, header,replace, http_response_code).The http_response_code).Theheader parameter specifies the URL or file name of the resource being redirected to, while the replaceand replaceandhttp_response_code parameters are optional.
The PHP Header function is considered to be the simplest and most secure method for implementing a PHP redirect. However, improper implementation can lead to significant server problems.
JavaScript through PHP is a method for implementing a PHP redirect using the JS window.location function. There are three methods to establish a PHP redirect using JS window.location: window.location.href, window.location.assign, and window.location.replace.
JavaScript through PHP redirects tend to have slower performance and rely on the end user’s browser having JavaScript enabled and downloaded. However, it can be a useful backup option if the PHP Header setup fails.
It is important to perform routine website audits to identify common issues such as redirect loops and chains, missing redirects or canonicals, and potential setup errors like using temporary redirects instead of permanent ones, and vice versa.
The choice between using the PHP Header function and JavaScript through PHP depends on the specific requirements of the project. Factors to consider include the need for a server-side or client-side redirect, the performance requirements of the project, and the potential for improper implementation leading to server problems.