htaccess redirige le domaine vers https, sous-domaine avec paramètre http

J’essaie de faire ça:

Forcer le https pour mon domaine principal.

http ou https://www.domain.com -> https://domain.com
http ou https://domain.com -> https://domain.com

c’est le codage htaccess pour le domaine

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

cela fonctionne bien

Mais pas pour les sous-domaines

Je veux résultat comme

 https://www.domain.com/index.php?username=abc => http://abc.domain.com http://www.domain.com/index.php?username=abc => http://abc.domain.com 

Et en supprimant toujours les www et https sur http.

mais aucune idée de la façon d’écrire du code htaccess pour le sous-domaine

Cela va d’abord vérifier si vous devez redirect vers un sous-domaine basé sur le paramètre username dans la chaîne de requête si vous demandez index.php. Ensuite, lors de la réécriture sur ce sous-domaine, il copiera la chaîne de requête complète avec, mais en omettant le paramètre nom d’utilisateur tout en conservant tous les autres parameters.

La deuxième partie vérifiera votre domaine principal avec ou sans www, puis imposera https, si nous n’avons pas déjà décidé de redirect vers un sous-domaine.

 RewriteEngine On #match either the www subdomain or no subdomain RewriteCond %{HTTP_HOST} ^(?:www.)?(?:domain\.com)$ #which request index.php RewriteCond %{REQUEST_URI} ^/index\.php #and contain a username paramater RewriteCond %{QUERY_STRING} ^(.*?)(?:^|&)username=([^&]+)(.*?)$ #then rewrite them to username.domain.com without the username parameter RewriteRule ^ http://%2.domain.com%{REQUEST_URI}?%1%3 [R,QSA,L] #match either the www subdomain or no subdomain RewriteCond %{HTTP_HOST} ^(?:www.)?(?:domain\.com)$ #which was requested without https RewriteCond %{HTTPS} off #then redirect to https RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]