PHP Redirect to URL / WebPage – Example Script

PHP redirect to url tutorial explains how to redirect a user from a url they entered to a different url or webpage using PHP. Explained with most basic example with samlpe  PHP redirect script.

php redirect to url localhost

PHP is a very simple language, its just a matter of few lines to 301 redirect  or any other redirect a url to another url. In php header () method is used to send a raw HTTP header.


You just need to copy and paste the following code in index.php file of any domain or directory you want to redirect using PHP. This is how a basic php redirect to url code looks.

< ?php
 Just paste the code above everything else even before body and head tag. The above code will redirect any domain or sub directory to, just change it to your desired location.

Note: – You should use the above code by creating a new empty php file and pasting the above code. If you want to paste this code in existing file with already some code then you must use the exit function of php like given in the example below :-

< ?php
 The exit function of php will tell the browser to stop executing command from now on and all the code written below this will not get executed. So make sure to paste this above everything in the file.

Note:- The PHP header() function must be called before everything otherwise the code will not work. So paste this code before any actual output is sent to the server. For example if you put some any other PHP function like $var, etc. See the following example :-

echo "wrong code example"; 
echo $var;
 Above code will not work as the header() function is called after some lines of code, so be cautious while using this.


How to send a HTTP status code while redirecting user to another url or site using PHP. This is helpful in SEO too to tell search engine that whether your site is redirected temporarily or moved permanently to another url. This helps in diverting all link juices to new url of an existing site. PHP redirect to url with http status codes works as below.

php redirect to url
Some common http status codes to be used with php redirect to url script

Well this is a 2 step procedure. here we go :-

  1. Create a sitefunctions.php file containing all HTTP status codes. (Just copy and paste from below)
  2. Crate a new index.php file with PHP redirect code as explained below on step 2.

|Step 1. |

In sitefunctions.php file we need to create a function called movepage() so that we can call it from index.php file. here we go :-

function movepage($num,$url){
   static $http = array (
       100 => "HTTP/1.1 100 Continue",
       101 => "HTTP/1.1 101 Switching Protocols",
       200 => "HTTP/1.1 200 OK",
       201 => "HTTP/1.1 201 Created",
       202 => "HTTP/1.1 202 Accepted",
       203 => "HTTP/1.1 203 Non-Authoritative Information",
       204 => "HTTP/1.1 204 No Content",
       205 => "HTTP/1.1 205 Reset Content",
       206 => "HTTP/1.1 206 Partial Content",
       300 => "HTTP/1.1 300 Multiple Choices",
       301 => "HTTP/1.1 301 Moved Permanently",
       302 => "HTTP/1.1 302 Found",
       303 => "HTTP/1.1 303 See Other",
       304 => "HTTP/1.1 304 Not Modified",
       305 => "HTTP/1.1 305 Use Proxy",
       307 => "HTTP/1.1 307 Temporary Redirect",
       400 => "HTTP/1.1 400 Bad Request",
       401 => "HTTP/1.1 401 Unauthorized",
       402 => "HTTP/1.1 402 Payment Required",
       403 => "HTTP/1.1 403 Forbidden",
       404 => "HTTP/1.1 404 Not Found",
       405 => "HTTP/1.1 405 Method Not Allowed",
       406 => "HTTP/1.1 406 Not Acceptable",
       407 => "HTTP/1.1 407 Proxy Authentication Required",
       408 => "HTTP/1.1 408 Request Time-out",
       409 => "HTTP/1.1 409 Conflict",
       410 => "HTTP/1.1 410 Gone",
       411 => "HTTP/1.1 411 Length Required",
       412 => "HTTP/1.1 412 Precondition Failed",
       413 => "HTTP/1.1 413 Request Entity Too Large",
       414 => "HTTP/1.1 414 Request-URI Too Large",
       415 => "HTTP/1.1 415 Unsupported Media Type",
       416 => "HTTP/1.1 416 Requested range not satisfiable",
       417 => "HTTP/1.1 417 Expectation Failed",
       500 => "HTTP/1.1 500 Internal Server Error",
       501 => "HTTP/1.1 501 Not Implemented",
       502 => "HTTP/1.1 502 Bad Gateway",
       503 => "HTTP/1.1 503 Service Unavailable",
       504 => "HTTP/1.1 504 Gateway Time-out"
   header ("Location: $url");
 You can just copy and paste the above code in sitefunctions.php file.

|Step 2|

Now this is as simple as above creating a new index.php file with basic PHP redirect code using PHP header()  function but with calling custom HTTP status code from sitefunctions.php file.

First we need to include sitefunctions.php file and then call movepage() as in the following example:-

/* Move page with 301 http status code*/
 This code tell search engines that the above site has been redirected or moved permanently to as we are using 301 redirect code in the above example. You can use any other HTTP status code as you need from sitefunctions.php file.


Well this is as simple as entering your sites url here :- redbot
Just go to above site and enter your url and output will be something like this :-

PHP redirect to url
Fig. : Illustrating sample Check HTTP Location url redirection with redbot tool

If you are using Apple OS X or Linux/Unix-like operating system, then just open the command window and check with curl command with -I switch as in following example

$ curl -I

The output will be something like this :-

HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Mon, 04 Aug 2014 23:57:57 GMT
Connection: keep-alive
X-Galaxy: Andromeda-2

Hope this tutorial helps you in the best way, don’t forget to share, like, comment and follow me if you like the above tutorial.