top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

How to redirect a page in php?

+2 votes
70 views
How to redirect a page in php?
posted Jan 11, 2016 by Ritika Sharma

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

Simple Redirection
To redirect the visitor to another page (particularly useful in a conditional loop), simply use the following code:

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

Where mypage.php is the address of the page to which you want to redirect the visitors. This address can be absolute and may also include the parameters in this format: mypage.php?param1=val1&m2=val2)

Relative/absolute path
Ideally, choose an absolute path from the root of the server (DOCUMENT_ROOT), the following format:

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

If ever the target page is on another server, you include the full URL:

<?php    
header('Location: http://www.example.com/forum/');    
?>   

By default, the type of redirection presented above is a temporary one. This means that search engines such as Google will not take into account for indexation. So if you want to notify the search engines that the page has been permanently moved to another location:

<?    
header('Status: 301 Moved Permanently', false, 301);    
header('Location: new_address');    
?>

So when you click on the link above, you are automatically redirected to this page. Moreover, it is a permanent redirection (Status: 301 Moved Permanently). So if you type the first URL in Google, you will automatically be redirected to the second link (that is: The search engine take into account the redirection).

Interpretation of PHP code
The PHP code located after the header() will be interpreted by the server, even if the visitor move to the address specified in the redirection, which means that in most cases you need a method of follow the header() function of the exit() function, in order to decrease the load of the server.

<?    
header('Status: 301 Moved Permanently', false, 301);    
header('Location: address');    
exit();    
?> 
answer Jan 14, 2016 by Manikandan J
Similar Questions
–1 vote

I want to display the number of views of my homepage and continuously increase the views whenever a user opens that page. How to achieve this using PHP and MYSQL?

Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...