Redirect to another web page using PHP
Posted by Quinn McHenry in PHP programming
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
View more articles by Quinn McHenry
The Conversation
Follow the reactions below and share your own thoughts.





October 15, 2008 at 4:39 am, Robin Pahwa said:
check your website it has no php script
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.
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)
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?
December 09, 2008 at 11:25 am, linu said:
hii is i possible to send variables using this
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?
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……….
April 19, 2009 at 12:44 pm, Anup pokhrel said:
I also got “Headers already sent” problem
May 30, 2009 at 10:45 am, asdas said:
chutad hai tu
November 07, 2009 at 3:09 am, shubham gupta said:
thanks and i solved my problems
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
June 01, 2010 at 11:25 am, Jackie said:
just add the url like a get variable and use $_GET to retrieve it.
November 24, 2009 at 10:37 pm, Dave said:
Thanks for the php redirect page code!
March 24, 2010 at 1:34 pm, Anonymous said:
Thanks for sharing
May 03, 2010 at 6:34 pm, Form builder said:
I see there are so many ways for redirect
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.
August 31, 2010 at 7:32 am, Adnan said:
In PHP tag, we should use the following java script
echo “location.href=’abc.php’;”;
December 20, 2010 at 4:13 pm, bosna said:
great man
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..
December 28, 2010 at 10:03 am, Gunn 'em down said:
what if javascript is disabled in a browser
January 07, 2011 at 4:21 pm, chintoo said:
thanks mate… i got my code working with it….
February 18, 2011 at 11:51 am, Gauri said:
hey this solution is awesome and works without header errors… Thanks a lot
March 31, 2011 at 6:44 am, My Offhegoes said:
Your solution working like a charm.. solve my problem..
October 07, 2010 at 5:22 am, Cleverlearncebu said:
This is 302, how about 301 redirect in PHP? some info please?
October 09, 2010 at 3:02 am, Info said:
thanks abunch
October 09, 2010 at 3:03 am, Himay said:
ok thanks, still having problems getting it to work…
December 27, 2010 at 11:57 am, Shoaib said:
java script with php waow jst working good short
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…
April 26, 2011 at 5:03 am, Sendilrrk said:
Please add ob_start() to your connection file.
July 01, 2011 at 10:57 am, the raging homosexual said:
this is disgraceful trolololololololol
July 01, 2011 at 10:58 am, raging homo sexual nose said:
looooooooooooooooooooooooooooooooooooooool spam
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!
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.
December 01, 2011 at 5:02 am, witek said:
Location must point to absolute URL with full hostname!
December 20, 2011 at 7:30 am, maged said:
thanks man you are great
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..??