This PHP tutorial helps to create a page redirection using PHP. You can use redirection for a page, redirect to a url, PHP 301 redirect etc. We provided you with multiple methods to redirect a web page with PHP.
There are two main types of redirects in PHP: header() redirects and meta tag redirects.
You can redirect a page using the PHP header method. We’ll discuss different scenarios to redirection in PHP. The header()
must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or form PHP.
An HTML redirect differs from a PHP redirect in that HTML is client-side while PHP is server-facing. This important distinction enables a PHP redirect to offer faster and more secure travel from one page to another.
The header()
function is a PHP built-in function for sending raw HTTP headers to the client. This PHP method can be used for redirection from one page to another, redirecting to a specific URL.
Syntax:
header( $header, $replace, $http_response_code )
The parameters are:
Let’s redirect to another page using php header()
method. The simple code:
<?php /* Redirect browser */header("Location: https://phpflow.com/"); /* Make sure that code below does not get executed when we redirect. */exit; ?>
In the above code, We have used the header() function to send a “Location” header to the browser, which tells it to redirect to the specified URL. The “exit” statement is used to prevent any additional output from being sent to the browser.
We can also create 301 redirect using the PHP header()
method. You can achieve this by following the code:
header("HTTP/1.1 301 Moved Permanently"); header ("Location: https://phpflow.com/"); exit;
After the header, You must use die()
or exit()
. If the die()
or exit()
functions are not placed after the header('Location:....')
, the script may continue to run, resulting in unexpected exception will throw.
Also, you can use the header() function with ob_start() and ob_end_flush().T this should be first line of your page like this:
<?php ob_start(); header(....) ?>
A windows.location
object is implemented in JavaScript and can be used to retrieve the current URL and route the browser to a different website.
<!DOCTYPE html> <html> <head> <title>window.location function</title> </head> <body> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = "URL: " + window.location.href + "</br>"; document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML + "Hostname: " + window.location.hostname + "</br>"; document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML + "Protocol: " + window.location.protocol + "</br>"; </script> </body> </html>
There are two main methods for doing this. To redirect from within the HTML section of your page.
Meta tag redirects to a web page using an HTML meta tag. It instructs the browser to automatically redirect to a different page.
We can redirect to a page using <meta> tag. Here’s an example:
<meta http-equiv="refresh" content="0;url=newpage.php">
In the above example, We have set a refresh time of 0 seconds, which tells the browser to immediately redirect to the specified URL.
window.location.replace
is a method in JavaScript that replaces the current page with a new one. Let’s redirect to the page using javascript.
window.location.replace("http://newpage.php/");
It is similar to the window.location.href
method, but instead of adding a new entry to the browser’s history, it replaces the current entry. This means that the user cannot use the “back” button to return to the previous page.
It prevents the user from returning to the previous page, such as after a form submission or a login page.
We learned many ways to redirect a web page. The header() redirects are generally preferred over meta tag redirects, as they are more reliable and provide better control over the redirect process. The header() redirects allow you to set additional headers and status codes, such as HTTP 301 or 302
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More
View Comments
hii sir I have a question
How to redirect a php with parameter
I have a forget form like that
header("Location: resetpass.php")->with($name, $email);
and I want anyone click on create new password so it will redirect reset password page with name and email
please give any solution
You can use query params : header("Location: resetpass.php?"name"=$name&"email"=$email);