Monday, 24 November 2014

PHP Redirect to another page (redirect header)

Aa a webmaster, it may happens that one day you realize one of your files has a name that does not fit the purpose anymore or that the file tree should be changed. It is relatively easy to change the links within your site, but it is much more difficult to change the external links to other sites. Furthermore, when changing the location of the pages, the search engines may send users to a faulty address or, worse it can reset all the SEO of the page. Fortunately, there is a simple solution: "Redirections", allows you to maintain navigational consistency and SEO of a site, even if all the files have been moved. 

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.kioskea.net/forum/');    
?>   

HTTP Headers

According to the HTTP protocol, the HTTP headers must be sent before any type of content, meaning that no character should be sent before the header, not even a empty space! 
In other words, header() function must be used before any HTML. 
For more information, read this article: Error "headers already sent by" :http://en.kioskea.net/faq/1259-headers-already-sent-by 

Temporary/permanent redirections

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');    
?>

Example

This page has the following code: 
<?      
  header('Status: 301 Moved Permanently', false, 301);    
  header('Location: /pc/imprimante.php3');    
  exit();      
?>

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();    
?> 

0 comments:

Post a Comment