Votre intervenant informatique sur la presqu'île de Quiberon, et au-delà

Rediriger une page wordpress sans plugin

Au moins trois solutions sont possibles:

  • Utiliser un plugin (Redirection),
  • faire la redirection dans le fichier .htaccess, ou bien
  • insérer le fragment de code suivant dans le corps du fichier functions.php de votre thème enfant. Votre page https://monsite/ancien_nom sera automatiquement redirigée vers https://monsite/nouveau_nom et vous pouvez ajouter de nombreux cas.
function mes_redirection() {
     if (isset($_SERVER['HTTPS']) &&
        ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) ||
        isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
        $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
        $protocol = 'https://';
        }
        else {
        $protocol = 'http://';
    }

    $currenturl = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    $currenturl_relative = wp_make_link_relative($currenturl);
    switch ($currenturl_relative) {
         case 'ancien_nom':
            $urlto = home_url('nouveau_nom');
            break;
         default:
            return;
        }
    
    if ($currenturl != $urlto){
        exit( wp_redirect( $urlto ) );
}
}
add_action( 'template_redirect', 'mes_redirections' );