Redirections de pages

Codes HTTP

Redirection PHP :

  1. <?php
  2. header('Location: '.get_bloginfo('url'));
  3. exit;

Redirection avec un .htaccess :

Redirect permanent : Ceci envoie un code HTTP 301 redirection permanente qui informe les navigateurs, et surtout les moteurs de recherche, qu'il faut mettre à jour leurs liens vers la nouvelle adresse.

Attention: Ne fonctionne pas pour faire pointer votre site dans un sous dossier. Pour cela utiliser DirectoryIndex?

Pour rediriger le site entier vers une nouvelle adresse :

  1. Redirect permanent / http://nouveau-site.tld/

Ex :

  1. # Redirections pour le référencement
  2. <IfModule mod_alias.c>
  3. Redirect 301 /39/un_media_totalement_durable.html http://jmlc.info
  4. Redirect 301 /101/le_livre_de_lanniversaire_de_votre_entreprise.html http://jmlc.info
  5. </IfModule>

Pour changer un repertoire/fichier :

  1. Redirect permanent /ancien_repertoire http://nouveau-site.tld/nouveau_repertoire
  2. Redirect permanent /ancien_fichier.php http://site.tld/nouveau_fichier.php

Global http to https :

  1. # SSL/HTTPS
  2. <IfModule mod_alias.c>
  3. RewriteEngine On
  4. RewriteCond %{HTTPS} !on
  5. RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
  6. </IfModule>

It's better to use a virtualhost to do that but that's the best way to do it on shared hosting : Doc StackOverflow

Plus d'infos et de redirections : Source
Documentation apache

Redirection javascript

jQuery is not necessary, and window.location.replace(…) will best simulate an HTTP redirect.
It is better than using window.location.href =, because replace() does not put the originating page in the session history, meaning the user won't get stuck in a never-ending back-button fiasco. If you want to simulate someone clicking on a link, use location.href. If you want to simulate an HTTP redirect, use location.replace.
For example:

  1. // similar behavior as an HTTP redirect
  2. window.location.replace("http://stackoverflow.com");
  3.  
  4. // similar behavior as clicking on a link
  5. window.location.href = "http://stackoverflow.com";

Source