Redirect to another web page using PHP

Redirection from one web page to another is a handy tool. This recipe describes the syntax that causes the client to redirect to another URL.


The following php script redirects the user to /index.php within the same site:

<? header('Location: /index.php'); ?>

The redirection target can be any suitable URL as could be used in the href parameter of an address tag (such as http://www.tech-recipes.com/index.php). The header function sends raw header data to the client web browser and in this case sends a 302 status code which instructs the browser to redirect to the given location.

 

About Quinn McHenry

Quinn was one of the original co-founders of Tech-Recipes. He is currently crafting iOS applications as a senior developer at Small Planet Digital in Brooklyn, New York.
View more articles by Quinn McHenry

The Conversation

Follow the reactions below and share your own thoughts.

36 Responses to “Redirect to another web page using PHP”

  1. October 15, 2008 at 4:39 am, Robin Pahwa said:

    check your website it has no php script

    Reply

    • October 15, 2008 at 4:48 am, Quinn McHenry said:

      Thanks Robin! We recently migrated to WordPress and some of our code blocks didn’t quite make it.

      Reply

  2. November 13, 2008 at 7:28 am, saurabh said:

    is there any alternative of this header , so that next page is redirected without any event(not by hyperlin)

    Reply

    • March 20, 2009 at 6:37 am, bassline77 said:

      Yeah, I’ve got the some problem, when i redirect my page this way, it gives me a “headers already sent” error. Is there an alternative?

      Reply

  3. December 09, 2008 at 11:25 am, linu said:

    hii is i possible to send variables using this

    Reply

  4. January 14, 2009 at 8:22 am, vinu said:

    hi thanks for this article.
    but my problem is that header() is used for redirecting once and i want to redirct page with parameter no. of times what the solution for this?

    Reply

  5. March 12, 2009 at 4:53 am, Malar said:

    Hi thanks for this article.This is very useful and easily clear my doubts.Thank you……….

    Reply

  6. April 19, 2009 at 12:44 pm, Anup pokhrel said:

    I also got “Headers already sent” problem

    Reply

  7. May 30, 2009 at 10:45 am, asdas said:

    chutad hai tu

    Reply

  8. November 07, 2009 at 3:09 am, shubham gupta said:

    thanks and i solved my problems

    Reply

  9. November 12, 2009 at 2:30 am, Flight said:

    i want to redirect my PHP page using variable instead of fixed url
    Thanks so much

    Reply

    • June 01, 2010 at 11:25 am, Jackie said:

      just add the url like a get variable and use $_GET to retrieve it.

      Reply

  10. November 24, 2009 at 10:37 pm, Dave said:

    Thanks for the php redirect page code!

    Reply

  11. March 24, 2010 at 1:34 pm, Anonymous said:

    Thanks for sharing

    Reply

  12. May 03, 2010 at 6:34 pm, Form builder said:

    I see there are so many ways for redirect

    Reply

  13. May 25, 2010 at 12:55 pm, Pratik Dangol said:

    can i get code for data inserting and displaying and recently update the chandes. like facebook commet in wall, while we click publish it will insert data and display the latest updated data of database.

    Reply

  14. August 31, 2010 at 7:32 am, Adnan said:

    In PHP tag, we should use the following java script
    echo “location.href=’abc.php’;”;

    Reply

    • December 20, 2010 at 4:13 pm, bosna said:

      great man

      Reply

    • December 20, 2010 at 4:14 pm, bosna said:

      This is the easiest way i guess.. No header problem at all.. keep up the good work..

      Reply

    • December 28, 2010 at 10:03 am, Gunn 'em down said:

      what if javascript is disabled in a browser

      Reply

    • January 07, 2011 at 4:21 pm, chintoo said:

      thanks mate… i got my code working with it….

      Reply

    • February 18, 2011 at 11:51 am, Gauri said:

      hey this solution is awesome and works without header errors… Thanks a lot

      Reply

    • March 31, 2011 at 6:44 am, My Offhegoes said:

      Your solution working like a charm.. solve my problem..

      Reply

  15. October 07, 2010 at 5:22 am, Cleverlearncebu said:

    This is 302, how about 301 redirect in PHP? some info please? :)

    Reply

  16. October 09, 2010 at 3:02 am, Info said:

    thanks abunch

    Reply

  17. October 09, 2010 at 3:03 am, Himay said:

    ok thanks, still having problems getting it to work…

    Reply

  18. December 27, 2010 at 11:57 am, Shoaib said:

    java script with php waow jst working good short

    Reply

  19. April 04, 2011 at 12:15 am, Lifemate 2 said:

    you know what! I already knew how to redirect but by looking at ur code … that reminds me to the url which I had entered ! the only thing was the wrong page … anyways thanks…

    Reply

  20. April 26, 2011 at 5:03 am, Sendilrrk said:

    Please add ob_start() to your connection file.

    Reply

  21. July 01, 2011 at 10:57 am, the raging homosexual said:

    this is disgraceful trolololololololol

    Reply

  22. July 01, 2011 at 10:58 am, raging homo sexual nose said:

    looooooooooooooooooooooooooooooooooooooool spam

    Reply

  23. August 27, 2011 at 4:05 am, Ty said:

    Hi,

    What “address” do you put in the initial webpage html to call the php file?

    The php file runs and redirects when I type it into the address of IE, but I can’t seem to call the file from the initial page!

    Reply

  24. September 19, 2011 at 7:01 am, Stephen said:

    hi
    I need to redirect my old page to a new page, the page is made in PHP. so please tell me what is the code to redirect from one page to another page.

    Reply

  25. December 01, 2011 at 5:02 am, witek said:

    Location must point to absolute URL with full hostname!

    Reply

  26. December 20, 2011 at 7:30 am, maged said:

    thanks man you are great

    Reply

  27. March 07, 2012 at 1:45 am, joed said:

    help me pls. if i have a 2 webpage the first is for admin and the second for the student. how to put the video into second webpage with the use of the first page..??

    Reply

Leave a Reply

You may also like-

Cannot modify header information - headers already sentThis error message is commonly seen by programmers starting to use PHP. Understanding why this error occurs will help find the solution. PHP handles ...