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. If so, 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.
When changing the location of the pages, 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.
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)
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:
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.
Interpretation of 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: