Direkt zum Inhalt
Drupal 10, Umleiten von http-Anfragen auf https

Die Umleitung von unsicheren (http) auf https-Anfragen kann auf verschiedene Weise erfolgen. Wir schlagen Ihnen einen einfachen Ansatz über die .htaccess-Datei vor.


Sie müssen den folgenden Code in die .htaccess-Datei
einfügen
 

   # NEW CODE HERE #
   RewriteCond %{HTTPS} off
   RewriteCond %{HTTP:X-Forwarded-Proto} !https
   RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
   # END NEW CODE #

Ihre Datei sollte wie folgt aussehen:

...
# 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}]
...

 

Umleitung auf eine Domain mithilfe der .htaccess-Datei

Um eine doppelte Indexierung zu vermeiden, ist es wichtig, alle Anfragen entweder auf www.monsite.com oder monsite.com umzuleiten.
Für habeuk haben wir uns für habeuk.com entschieden. laugh.
 

  ##### 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]

- Die [NC]-Flagge: unsensibel im Feld.
- Die Flagge [L]: ignoriert die Regeln im Anschluss.
 

Wie man Änderungen an der .htacss-Datei persistent macht

Die Änderungen, die Sie zuvor vorgenommen haben, werden verschwinden, wenn Sie composer verwenden.

...
Scaffolding files for drupal/core:
  - Copy [web-root]/.htaccess from assets/scaffold/files/htaccess
...

Um dieses Problem zu lösen, gibt es zwei Ansätze:

  1. Deaktivieren Sie die Aktualisierung der .htaccess-Datei, indem Sie Folgendes hinzufügen:  "[web-root]/.htaccess": false
    Dieser Ansatz wird nicht empfohlen, da Sicherheitsaktualisierungen nicht mehr angewendet werden.

    ...
    "extra": {
            "drupal-scaffold": {
                "locations": {
                    "web-root": "web/"
                },
    	    "file-mapping": {
                	"[web-root]/.htaccess": false
                }
            },
    ...
  2. Wir fügen den benutzerdefinierten Code nach jeder Ausführung hinzu: composer install, composer update und composer require.

    Erstellen Sie eine Datei für die benutzerdefinierten Requettes :

    nano web/htaccess_custom.conf

    Fügen Sie den folgenden Inhalt hinzu:

    # =============================================
    # 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
    # =============================================
    

    Erklärung
    - Prüft, ob die angeforderte Domain monsite.com mit oder ohne www. ist, und die folgende Bedingung wird als Alternative ausgewertet.
    - Prüft, ob die Domain genau www.monsite.fr ist.
    - Prüft, ob die Verbindung nicht HTTPS ist und die folgende Bedingung wird als Alternative ausgewertet.
    - Prüft, ob die Website hinter einem Proxy (z. B. Cloudflare, AWS ELB) liegt und das verwendete Protokoll nicht https ist.
    -  Dann leiten wir auf die Domain /monsite.de weiter.

    Erstellen Sie eine Bash-Datei :

    nano scripts/post-update-htaccess.sh

    Fügen Sie den folgenden Inhalt hinzu:

    #!/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"

    Fügen Sie die Ausführungsrechte hinzu:

    chmod +x scripts/post-update-htaccess.sh

    Bearbeiten wir die Datei composer.json:

    ...
       "scripts": {
          "post-install-cmd": [
             "scripts/post-update-htaccess.sh"
          ],
          "post-update-cmd": [
             "scripts/post-update-htaccess.sh"
          ]		
       },
    ...

    Dieser Ansatz wird empfohlen, da er die Möglichkeit bietet, von Sicherheitsaktualisierungen zu profitieren.

Profile picture for user admin Stephane K

Écrit le

Il y'a 1 Jahr
Modifié
Il y'a 2 Wochen
Lädt...
WhatsApp
Support Habeuk : +237 694 900 622
WhatsApp Send