How to make redirect in PHP?

How to make redirect in PHP?

As a webmaster, you may be faced with a situation where one of your files has a name that does not fit its purpose or you need to rename a file tree. It is relatively easy to change the links within your site, though it is much more difficult to change any external links to other sites. You can use redirections to fix this.

When changing the location of a page, search engines may send users to a faulty address. Even worse, the page's SEO may be completely reset. Fortunately, there is a simple solution that utilizes redirections to allow you to maintain the navigational consistency and SEO of your site — even if all the files have been moved.

How to redirect headers?

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

In this case, mypage.php is the address of the page to which you would like 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

When dealing with relative or absolute paths, it is ideal to choose an absolute path from the root of the server (DOCUMENT_ROOT). Use 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: https://ccm.net/forum/    
?>   

HTTP Headers

According to HTTP protocol, HTTP headers must be sent before any type of content. This means that no characters should ever be sent before the header — not even an empty space!

For more information, read our article on how to send headers.

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 the redirection into account when indexing.

If you would like to notify search engines that a page has been permanently moved to another location, use the following code:

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

For example, this page has the following code:

<?      
  header('Status: 301 Moved Permanently', false, 301);    
  header('Location: /pc/imprimante.php3');    
  exit();      
?>

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 into Google, you will automatically be redirected to the second, redirected link.

How to interpret PHP code?

The PHP code located after the header() will be interpreted by the server, even if the visitor moves to the address specified in the redirection. In most cases, this means that you need a method to 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();    
?> 
Around the same subject

Web