J’utilise Amazon Beanstalk donc mon infrastructure est
USER --(HTTPS)--> BEANSTALK BALANCER --(HTTP)--> EC2 INSTANCES
Maintenant, si les utilisateurs vont à
mydomain.com www.mydomain.com https://mydomain.com http://mydomain.com
Je veux toujours redirect vers
https://www.mydomain.com
J’ai le https qui fonctionne mais pour une raison quelconque, la redirection www
ne fonctionne pas lorsque https://mydomain.com
directement à la page https://mydomain.com
Options +FollowSymlinks -MultiViews -Indexes RewriteEngine On RewriteRule ^(.*)/$ /$1 [L,R=301] # If not on www., redirect to www. on SSL (does not work when accessing https://mydomain.com) RewriteCond %{HTTP:X-Forwarded-Proto} !https RewriteCond %{REQUEST_URI} !^/index.php/status$ RewriteCond %{HTTP_HOST} !^www #RewriteCond %{HTTP_HOST} ^mydomain\.com$ RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L] # If not on SSL(ish), redirect to SSL (works) RewriteCond %{HTTP:X-Forwarded-Proto} !https RewriteCond %{REQUEST_URI} !^/index.php/status$ RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L] #RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R,L] RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond $1 !^(index\.php|robots\.txt) RewriteRule ^(.*)$ index.php/$1 [L]
Plusieurs RewriteCond
sont des opérations AND
logiques par défaut. Vos deux conditions vérifient que le protocole transféré n’est pas https AND
l’hôte demandé ne commence pas par www
. Votre URL d’entrée https://example.com
(avec HTTPS mais pas www) ne remplit pas ces deux conditions, donc les règles ne s’exécutent pas.
Étant donné que la première redirection consiste à appliquer le www
là où il n’y en a pas déjà, vous n’avez pas besoin de vérifier https
sur cette passe. Il suffit de vérifier la présence de www
et de redirect vers l’hôte préféré, avec https://
.
# Do the SSL redirect first to protect your users # If not on SSL(ish), redirect to SSL (works) RewriteCond %{HTTP:X-Forwarded-Proto} !https RewriteCond %{REQUEST_URI} !^/index.php/status$ RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L] # If you don't mind a hard-coded domain here, you can avoid a second # redirect if the www is missing... # RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L] # Then add the www. # Do not check the protocol here. RewriteCond %{REQUEST_URI} !^/index.php/status$ RewriteCond %{HTTP_HOST} !^www RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
Le résultat ci-dessus se traduit peut-être par deux redirections pour l’utilisateur final. Il est possible de regrouper les deux en un seul bloc, en utilisant un groupe optionnel ()
sur le site www.
et capturer le rest de l’hôte dans %1
pour l’utiliser dans RewriteRule
.
Ceci n’a pas été testé …
# First condition is [AND] and just serves to grab the requested host # optional, non-capture group for www. if present # and a capture group into %1 for everything after www. no matter what RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC] # Next 2 conditions are OR'd together # Check for https RewriteCond %{HTTP:X-Forwarded-Proto} !https [OR] # Or the host doesn't start with www. RewriteCond %{HTTP_HOST} !^www [NC] # Rewrite as HTTPS, using the part of the domain following www. # whether or not there was a www. originally... RewriteRule ^(.*)$ https://www\.%1/$1 [L,R=301]