PHP Redirect To Another URL / Web Page Script Example

See all PHP related FAQ
How do I redirect with PHP script? How can I use a PHP script to redirect a user from the url they entered to a different web page/url?

Tutorial details
Difficulty level Easy
Root privileges No
Requirements php
Est. reading time 2 minutes
Under PHP you need to use header() to send a raw HTTP header. Using headers() method, you can easily transferred to the new page without having to click a link to continue. This is also useful for search engines. Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

HowTo redirect with PHP

You can easily redirect using following header("Location: ....) syntax:

/* Redirect browser */
/* Make sure that code below does not get executed when we redirect. */

The header() is used to send a raw HTTP/1.1 specification specific header. header() must be called before any actual output is sent, the following example will not work:

echo "Hello world";
echo $var;
* Remember that header() must be called before any actual output is sent,
* either by normal HTML tags, blank lines in a file, or from PHP.

PHP Redirect with HTTP Status Code

Create a sample function called movePage() in sitefunctions.php (note I’m not the author of the following I found it somewhere else on the Internet):

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");

First include sitefunctions.php and than call movePage() as follows:

/* Move page with 301 http status code*/

How do I test url redirection?

If you are using Apple OS X or Linux/Unix-like operating system, open the Terminal app and type curl command as follows:
$ curl -I your-url-here
$ curl -I

Sample outputs:

HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Mon, 30 Dec 2013 22:31:37 GMT
Connection: keep-alive
X-Galaxy: Andromeda-2

If you are on MS-Windows or do not want to use command line try redbot tool:

Fig. 01:  Check HTTP Location url redirection with redbot tool

Fig. 01: Check HTTP Location url redirection with redbot tool

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

Comments on this entry are closed.

  • Giftvincy May 15, 2013 @ 12:28

    Really clean and informative article.
    But, I have a doubt about the purpose of using exit command?

    Will it be executed or just to mention that the following lines are not going to be executed?

  • james Jun 3, 2013 @ 11:43

    Hello :)

    Apologies if this sounds particularly dense owing to the simplicity of the task but…

    My php complete with link to a new page is within an Iframe (on a Wix site). I’d like to get this link to open in the parent page. Currently the link opens but within the Iframe. Does anyone know how to do this?

    Any help would be very much appreciated!

  • mahi Aug 17, 2013 @ 6:38

    “how to display data from different table on same page but remove previous data on that same page means when click on any link always display content on same page and omit previously shown data on that page in php.”

  • Harsh Oct 7, 2013 @ 10:56






    in the above code my databse get store but it is not redirecting on the url i mention in the header pls can anyone help me on this on my localwap server it runs pefect but i tried to uplado on 3 diff server i get same error

  • savi Oct 15, 2013 @ 8:36

    i m not geting error bt page is not redirected to profile,php
    below is my code

    	$sql="select * from register Where ConfirmEmail='$email_id' AND ConfirmPassword='$password'";
    	die("An Error".mysql_error());
    		$_SESSION["mid"]= $row["ID"];
    		$q = "UPDATE register SET Thislogin=NOW(),logtime='$time',login_status='$login_status' WHERE ConfirmEmail='$email_id' AND ConfirmPassword='$password'";
    			$status_msg="Your profile is wating for approval";	
    			$msg="Invalid Email ID & Password Please Re-enter!";
  • Johan de Kock Oct 20, 2013 @ 5:03

    Using the mentioned php-script (header();), how do one redirect to different domains/URL’s based on the specific user login, e g if Mr A or Mrs A login, he or she is redirected to http://www.mypage_A/, and if Mr B or Miss B login, he or she is redirected to http://www.mypage_B/, etc. (Assuming that these users will each use different computers (with different IP-addresses), different (physical) locations and blocked the use of cookies, and the only reliable way to determine to which domain/URL to redirect them to is based on their usernames (and passwords) (at login).

  • Altaf Hussain Oct 28, 2013 @ 8:01

    I am using facebook API for one of my project. But when I redirect to, it sends me to… but I dont want so. I needed the web version. How to redirect to its web version. Please suggest.

  • Steve Dec 9, 2013 @ 9:36

    Thank you for this information. I now have a fully functional php email script in a popup window that redirects when finished and self closes. Awesome!

    All I need now is to figure out how to get php to parse the attachment using the mail() function. I have been bashing my head over that one for a few days now as the coding examples online do not work.

Comments are closed. Still have questions? Post it on our forum