Test de redirection
FR EN ES

Testez vos redirections web et réponses HTTP



La redirection en PHP

PHP envoie des entêtes HTTP via la fonction header(). Elle doit être appelée avant tout affichage, y compris les espaces ou sauts de ligne situés avant la balise <?php.


Redirection permanente 301 en PHP

À placer au tout début du fichier :

Syntaxe moderne (PHP 5.4+, recommandée) :

<?php
http_response_code(301);
header("Location: https://www.exemple.net/nouvelle-page.php");
exit();
?>

Syntaxe alternative (compatible toutes versions de PHP) :

<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: https://www.exemple.net/nouvelle-page.php");
exit();
?>


Redirection temporaire 302 en PHP

Par défaut, PHP envoie un code 302 quand on utilise header("Location: ...") :

<?php
header("Location: https://www.exemple.net/repertoire/page.php");
exit();
?>

Pour être explicite (recommandé) :

<?php
http_response_code(302);
header("Location: https://www.exemple.net/repertoire/page.php");
exit();
?>


Redirection conditionnelle en PHP

Rediriger selon la langue du navigateur :

<?php
$langue = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
if ($langue == 'en') {
    header("Location: https://www.exemple.net/en/");
    exit();
}
?>

Rediriger en conservant les paramètres GET :

<?php
$params = !empty($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : '';
http_response_code(301);
header("Location: https://www.exemple.net/nouvelle-page.php" . $params);
exit();
?>