PHP is a server-side programming language that produces HTML and HTTP content. The HTTP protocol provides the Location
header which is used to respond to a user request with a new URL that should be used and redirected. PHP can be used to create the Location header for HTTP.
PHP Header Location Syntax
The PHP header()
method syntax is like below. Simly the header()
method accepts a single string which is an HTTP header that is Location
in this case.
header(HEADER);
- HEADER is an HTTP Location header string which starts with
Location
.
Redirect Location Header in PHP
The PHP provides the header()
method in order to add, change or remove attributes from the HTTP header. The location is provided as a URI where it starts with the Location
header like below.
<?
header("Location:https://www.wisetut.com");
?>
The Location header can be also in different protocols which can be http
, ftp
etc.
<?
header("Location:ftp://www.wisetut.com");
?>
Redirect Another Website
The PHP header() method can be used to redirect to another website easily. In the following example, we redirect into the website https://www.linuxtect.com
.
<?
header("Location:https://www.linuxtect.com");
?>
Redirect Another URI in the Same Website
Another useful case for the PHP header() redirect is the ability to redirect different URI in the same site. As example we are navigating the website “https://www.wisetut.com/abc” and navigate to the “https://www.wisetut.com/def”. We just use the “/def” as the URI like below.
<?
header("Location:/def");
?>