
When the header() function is used with one argument, then 302 is used as the default HTTP code.Īfter executing the code, The URL is redirected to the location after 2 seconds. Here, the die() function is used to terminate the script.
Redirector php url code#
Example-1: Redirect URL with default status codeĬreate a PHP file with the following code that will redirect to the new location after waiting for 2 seconds. The $http_response_code is used to store a specific response code that will send to the user. The $replace defines whether to replace the previous similar header, and the value of this argument is Boolean. The $header is used to store the header string that contains the location of the redirection. The first argument is mandatory, and the last two arguments are optional. The syntax of this function is shown below. It is a built-in PHP function to send the raw HTTP header to the client. How the header() function can be used in PHP to redirect URL from one page to another page is shown in this tutorial. The URL redirects in PHP by using the header() function. It will help the user to know about the new URL by redirection, and the user will send a request to the new location to get the desired content. When the user sends a request for a page to the server that does not exist or of a page location that has changed, then the server will send the information about the new URL with 301 or 302 HTTP code. The redirection is required for many purposes, such as switching from HTTP to HTTPS, changing domain, etc. For more information, you can visit the PHP redirect documentation at PHP redirect.The URL of the user’s browser can be changed from one location to another by using redirection. I hope you have now enough knowledge on how PHP redirection works.
Redirector php url how to#
In the above tutorial, we have learned how to redirect URL from one page to another with PHP header() function. Then, check the URL redirection by visiting the URL You should see the following page:Ībove page indicates that page1.php will redirects after 10 seconds. You can also redirect PHP page to another page with refresh function instead of Location.įor example, create a page1.php that redirect to page2.php after 10 seconds: Then, you can test it with your web browser or Curl command. Save and close the file when you are finished. You can achieve this by editing the page1.php file as shown below: But, it is better to redirect to an absolute URL. In the above examples, The URL does not contain a hostname, this will work on modern browser. Then, check PHP redirect again with the Curl command: If you want to redirect page1.php to another site with response code 301 then edit the php1.php file with the following contents: You can also test the URL redirection with Curl command:Ĭurl -I You should see the following output:īy default, search engine replies with the default response code 302 while Browser reply with the response code 30x. Next, you can test the URL redirection by visiting the page1.php at URL You will be redirected to the page2.php as shown below: Save and close the file, when you are finished. In this example, we will create a page1.php that contains code that issues a redirect and page2.php that contains just HTML. In this section, we will give you a quick example of how to create a redirect using PHP. This is used to hold the HTTP response code Indicates the header should replace a previous similar header, or add a second header of the same type This is used to hold the header string to send Header( header, replace, http_response_code )

The header() function is an inbuilt function in PHP which is used to send a raw HTTP header to the client.īasic syntax of header() function in PHP redirect is shown below: To use a redirect in PHP, we use a header() function.

In this tutorial, we will learn how to redirect PHP page with the header() function. There are several reasons to use PHP redirect, including, Merger of two websites, Change of business name, Redirect traffic to updated content and many more. Redirection is very important and frequently used in Web Development phases. This will be helpful in such circumstances when you want to redirect a certain page to a new location, change the URL structure of a site and redirect users to another website. PHP redirect is a method used to redirect a user from one page to another page without clicking any hyperlinks.
