Redirect to another web page with php


elephpant

Simple redirection

To redirect the visitor to another page (especially useful in a conditional loop), just use the following code: 

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


Where my page represents the address of the page to which you want to redirect. This address can be absolute and can have parameters of the form mypage.php? param1 = val1¶m2 = val2). 

Relative / absolute path

Theoretically, it is better to prefer an absolute path from the server root (DOCUMENT_ROOT), as follows: 

<?php
header('Location: /repertoire/mapage.php');
?>


If the landing page is on another server, then indicate the full URL, as follows: 

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

HTTP headers

Redirects are HTTP headers. But, according to the HTTP protocol, HTTP headers must be sent before any other type of content, which means that no characters must be sent before the call of the header function, not even a space! 

In other words, the header () function must necessarily be used before any HTML code

Temporary / permanent redirects

By default, the type of redirect presented above is a temporary redirect. Which means that search engines like Google don't take it into account for rankings. 

Therefore, if we want to indicate to search engines that page A is now in location B, we must use the following code at the beginning of the PHP file corresponding to page A: 

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

Interpretation of PHP code

The PHP code located after the call to the header () function will be interpreted by the server, even if the visitor has already left the address specified in the redirection, which means that in most cases we will be interested in putting the exit function () after the header () function to not make the server work for pleasure: 

<?
header('Status: 301 Moved Permanently', false, 301);
header('Location: direccion);
exit();
?>
Previous How to create an auto-responder in cPanel
Next Remove virus from my cPanel

No Comment

Leave a reply

Your email address will not be published. Required fields are marked *