Redirection avec .htaccess

Guide de configuration des redirections à l'aide du fichier de configuration .htaccess.

Le fichier .htaccess vous permet de configurer des redirections (301 ou 302) au niveau du site sans modifier le code de votre application. Il se trouve dans le répertoire racine de votre site (généralement /public_html ou équivalent). S'il n'existe pas, créez-le (notez que son nom commence par un point).

Toutes les règles nécessitent que le module mod_rewrite soit activé :

RewriteEngine On

Redirection de HTTP vers HTTPS

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Variante alternative (lorsque le serveur utilise l'en-tête X-HTTPS) :

RewriteEngine On
RewriteCond %{HTTP:X-HTTPS} !1
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

Redirection de HTTPS vers HTTP

RewriteEngine on
RewriteCond %{HTTPS} on
RewriteRule ^.*$ http://%{SERVER_NAME}%{REQUEST_URI}

Redirection d'une page vers une autre

Redirect 301 /test-1/ http://site.ru/test-2/

Alternative avec Rewrite :

RewriteCond %{REQUEST_URI} ^/test/$
RewriteRule ^.*$ http://site.ru/new-test/? [R=301,L]

Redirection de www vers non-www (le miroir principal est le domaine non-www)

RewriteCond %{HTTP_HOST} ^www\.(.*)$
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]

Redirection de non-www vers www (le miroir principal est le domaine www)

RewriteCond %{HTTP_HOST} ^([^www].*)$
RewriteRule ^(.*)$ http://www.%1/$1 [L,R=301]

Redirection des URLs avec slash final vers sans slash final (ensemble du site)

RewriteCond %{REQUEST_URI} !\?
RewriteCond %{REQUEST_URI} !\&amp
RewriteCond %{REQUEST_URI} !\=
RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_URI} ![^\/]$
RewriteRule ^(.*)\/$ /$1 [R=301,L]

Redirection des URLs sans slash final vers avec slash final

RewriteCond %{REQUEST_URI} !\?
RewriteCond %{REQUEST_URI} !\&amp
RewriteCond %{REQUEST_URI} !\=
RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_URI} !\/$
RewriteRule ^(.*[^\/])$ /$1/ [R=301,L]

Redirection combinée : non-www + slash final

RewriteCond %{REQUEST_URI} !\?
RewriteCond %{REQUEST_URI} !\&
RewriteCond %{REQUEST_URI} !\=
RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_URI} !\/$
RewriteCond %{HTTP_HOST} ^www\.(.*)$
RewriteRule ^(.*)$ http://%1/$1/ [L,R=301]

RewriteCond %{REQUEST_URI} !\?
RewriteCond %{REQUEST_URI} !\&
RewriteCond %{REQUEST_URI} !\=
RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_URI} !\/$
RewriteCond %{HTTP_HOST} ^([^www].*)$
RewriteRule ^(.*)$ http://%1/$1/ [L,R=301]

Redirection combinée : www + slash final

RewriteCond %{REQUEST_URI} !\?
RewriteCond %{REQUEST_URI} !\&
RewriteCond %{REQUEST_URI} !\=
RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_URI} !\/$
RewriteCond %{HTTP_HOST} ^www\.(.*)$
RewriteRule ^(.*)$ http://www.%1/$1/ [L,R=301]

RewriteCond %{REQUEST_URI} !\?
RewriteCond %{REQUEST_URI} !\&
RewriteCond %{REQUEST_URI} !\=
RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_URI} !\/$
RewriteCond %{HTTP_HOST} ^([^www].*)$
RewriteRule ^(.*)$ http://www.%1/$1/ [L,R=301]

Redirection combinée : www + sans slash final

RewriteCond %{REQUEST_URI} ^\/$
RewriteCond %{HTTP_HOST} ^([^www].*)$
RewriteRule ^(.*)$ http://www.%1/$1 [L,R=301]

RewriteCond %{REQUEST_URI} !\?
RewriteCond %{REQUEST_URI} !\&
RewriteCond %{REQUEST_URI} !\=
RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_URI} \/$
RewriteCond %{HTTP_HOST} ^www\.(.*)$
RewriteRule ^(.*)\/$ http://www.%1/$1 [L,R=301]

RewriteCond %{REQUEST_URI} !\?
RewriteCond %{REQUEST_URI} !\&
RewriteCond %{REQUEST_URI} !\=
RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_URI} !\/$
RewriteCond %{HTTP_HOST} ^([^www].*)$
RewriteRule ^(.*)$ http://www.%1/$1 [L,R=301]

Redirection combinée : non-www + sans slash final

RewriteCond %{REQUEST_URI} ^\/$
RewriteCond %{HTTP_HOST} ^www\.(.*)$
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]

RewriteCond %{REQUEST_URI} !\?
RewriteCond %{REQUEST_URI} !\&
RewriteCond %{REQUEST_URI} !\=
RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_URI} \/$
RewriteCond %{HTTP_HOST} ^www\.(.*)$
RewriteRule ^(.*)\/$ http://%1/$1 [L,R=301]

RewriteCond %{REQUEST_URI} !\?
RewriteCond %{REQUEST_URI} !\&
RewriteCond %{REQUEST_URI} !\=
RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_URI} !\/$
RewriteCond %{HTTP_HOST} ^([^www].*)$
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]

Rediriger uniquement site.ru/index.php (sans paramètres GET) vers le miroir principal site.ru

RewriteCond %{REQUEST_URI} /index.php
RewriteCond %{QUERY_STRING} ^\z
RewriteRule ^(.*)$ http://site.ru/? [R=301,L]

Supprimer index.php des URLs en conservant les paramètres GET

Exemple : site.ru/index.php?n=1 vers site.ru/?n=1

RewriteCond %{REQUEST_URI} /index.php
RewriteRule ^(.*)$ http://site.ru/ [R=301,L]

Redirection en masse pour index.php, index.html, index.htm (ex. Joomla)

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.(php|html|htm)\ HTTP/ 
RewriteRule ^(.*)index\.(php|html|htm)$ http://site.ru/$1 [R=301,L]

Rediriger une URL dynamique avec paramètre GET vers une page statique

Variante simple :

RewriteCond %{QUERY_STRING} ^id=229
RewriteRule ^.*$ /supermodel/? [R=301,L]

Avec page :

RewriteCond %{REQUEST_URI} /test/
RewriteCond %{QUERY_STRING} ^id=229
RewriteRule ^.*$ /supermodel/? [R=301,L]

Rediriger toutes les pages d'un domaine vers la page principale d'un autre domaine

RewriteCond %{REQUEST_URI} (.*)
RewriteRule ^(.*)$ http://site.ru/ [L,R=301]

Rediriger l'ensemble du site vers un nouveau domaine en conservant la structure des URLs

RewriteCond %{REQUEST_URI} (.*)
RewriteRule ^(.*)$ http://site.ru/$1 [L,R=301]

Nos produits et services

Hébergement webFonctionne sur des disques NVMe ultra-rapides. Convient aux sites de toute complexité.
Commande
VPSInfrastructure cloud flexible avec accès root complet.
Commande
Serveurs dédiésServeurs physiques pour une performance maximale.
Commande
Besoin d’aide?Nos ingénieurs vous aideront gratuitement pour n’importe quelle question en quelques minutesNous contacter