Drupal 10, rediriger les requêtes http vers https
La redirection des requêtes non sécurisé (http) vers https peut se faire de differente facon. Nous vous proposons une approche simple via le fichier .htaccess
Vous devez ajouter le code suivant dans le fichier .htaccess
# NEW CODE HERE #
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# END NEW CODE #
Votre fichier devrait ressembler à :
...
# Various rewrite rules.
<IfModule mod_rewrite.c>
RewriteEngine on
# NEW CODE HERE #
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# END NEW CODE #
# Set "protossl" to "s" if we were accessed via https://. This is used later
# if you enable "www." stripping or enforcement, in order to ensure that
# you don't bounce between http and https.
RewriteRule ^ - [E=protossl]
RewriteCond %{HTTPS} on
RewriteRule ^ - [E=protossl:s]
# Make sure Authorization HTTP header is available to PHP
# even when running as CGI or FastCGI.
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
...
Redirection vers un domaine en utilisant le fichier .htaccess
Afin d'eviter la double indexation, il est important de rediriger toutes les requetes soit sur www.monsite.com ou monsite.com.Pour habeuk, nous avons opter d'utiliser habeuk.com

##### NEW CODE HERE
# Redirection www vers non-www
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.monsite\.com [NC]
RewriteRule ^(.*)$ http://monsite.com/$1 [L,R=301]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.monsite\.com [NC]
RewriteRule ^(.*)$ https://monsite.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.monsite\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^monsite\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.monsite\.fr [NC]
RewriteRule ^(.*)$ https://monsite.fr/$1 [L,R=301]
- Le drapeau [NC] : inssencible à la case- Le drapeau [L] : ignore les regles à la suite.
Comment rendre persistance les modifications du fichier .htacss
Les modifications effectuez precedament vont disparaitre lorsque vous allez utiliser composer.
...
Scaffolding files for drupal/core:
- Copy [web-root]/.htaccess from assets/scaffold/files/htaccess
...
Pour ressoudre ce probleme on a 2 approches :
- On desactive la mise à jour du fichier .htaccess en ajoutant : "[web-root]/.htaccess": false
Cette approche n'est pas recommandé car les mises à jour de securité ne seront plus appliquées.... "extra": { "drupal-scaffold": { "locations": { "web-root": "web/" }, "file-mapping": { "[web-root]/.htaccess": false } }, ...
- On ajoute le code custom apres chaque execution : composer install, composer update, et composer require
Creer un fichier pour les requettes personnaliser :nano web/htaccess_custom.conf
# ============================================= # Règles de réécriture personnalisées pour Drupal # Ce fichier est inclus dans .htaccess après "RewriteEngine on" # ============================================= # ------------------------------------------------------------------------ # 1. Redirections HTTPS + Suppression des www (SEO-Friendly) # ------------------------------------------------------------------------ # Redirige TOUTES les variantes vers https://monsite.fr RewriteCond %{HTTP_HOST} ^(www\.)?monsite\.com$ [NC,OR] RewriteCond %{HTTP_HOST} ^www\.monsite\.fr$ [NC] RewriteCond %{HTTPS} off [OR] RewriteCond %{HTTP:X-Forwarded-Proto} !https RewriteRule ^(.*)$ https://monsite.fr/$1 [L,R=301] # ------------------------------------------------------------------------ # 2. Protection contre l'exploration des dossiers sensibles # ------------------------------------------------------------------------ # Bloque l'accès aux dossiers cachés (ex: .git, .env) RewriteRule ^(\.git|\.env|node_modules|vendor)/ - [F,L,NC] # ============================================= # FIN DU FICHIER # =============================================
- Si le domaine estmonsite.com
ouwww.monsite.com
- OU si le domaine estwww.monsite.fr
- OU si le protocole n’est pas sécurisé (HTTP au lieu de HTTPS)
- OU si le site est derrière un proxy (ex : Cloudflare, AWS ELB) et le protocole utilisé n’est pashttps
- Alors on redirige vers le domaine /monsite.fr
Creer un fichier bash :nano scripts/post-update-htaccess.sh
#!/bin/bash # Variables HTACCESS_FILE="web/.htaccess" CUSTOM_RULES_FILE="web/htaccess_custom.conf" TEMP_FILE="web/.htaccess.tmp" # Vérifie si les fichiers existent if [ ! -f "$HTACCESS_FILE" ]; then echo "Erreur: $HTACCESS_FILE introuvable!" >&2 exit 1 fi if [ ! -f "$CUSTOM_RULES_FILE" ]; then echo "Erreur: $CUSTOM_RULES_FILE introuvable!" >&2 exit 1 fi # Supprime les anciennes règles si elles existent (évite les doublons) sed '/# CUSTOM_RULES_START/,/# CUSTOM_RULES_END/d' "$HTACCESS_FILE" > "$TEMP_FILE" # Insère les nouvelles règles après "RewriteEngine on" awk ' /RewriteEngine on/ { print $0 print "# CUSTOM_RULES_START" while ((getline line < "'"$CUSTOM_RULES_FILE"'") > 0) { print line } print "# CUSTOM_RULES_END" next } { print } ' "$TEMP_FILE" > "$HTACCESS_FILE" # Nettoie le fichier temporaire rm -f "$TEMP_FILE" echo "✓ Règles personnalisées injectées dans $HTACCESS_FILE"
chmod +x scripts/post-update-htaccess.sh
... "scripts": { "post-install-cmd": [ "scripts/post-update-htaccess.sh" ], "post-update-cmd": [ "scripts/post-update-htaccess.sh" ] }, ...
Loading ...