.htaccess redirect loop when trying to add a forced HTTPS rule (Amazon Elastic Beanstalk)

I started getting this error after trying to enable a rule for forced HTTPS in a production environment. The BWC_ENV environment variable can have several different values: "prod", "stage", "ben_local", "nam_local", etc.

Here is my .htaccess:

RewriteEngine On # Force HTTPS RewriteCond %{HTTPS} !=on RewriteCond %{ENV:BWC_ENV} ^prod$ RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] # Parse the subdomain as a variable we can access in our scripts RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{HTTP_HOST} !^www RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)$ RewriteRule ^(.*)$ /$1?subdomain=%1 # Ditto for the path; map all requests to /index.php RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !robots.txt RewriteRule ^(.*)$ /index.php?path=$1 [L,QSA] # robots.txt - supply the correct one for each environment RewriteRule ^robots.txt$ /robots.prod.txt [NC] RewriteCond %{ENV:BWC_ENV} !prod RewriteRule ^robots.prod.txt$ /robots.stage.txt [NC] 

Edit

What more, if my .htaccess contains only the following, it will also trigger a redirect loop. Why could this be?

 RewriteEngine On RewriteCond %{HTTPS} !=on RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 
+4
source share
2 answers

It turns out that this is Amazon load balancing balancing. You must use the Amazon X-Forwarded-Proto header to accomplish this:

 RewriteEngine On RewriteCond %{HTTP:X-Forwarded-Proto} !https RewriteRule !/status https://%{SERVER_NAME}%{REQUEST_URI} [L,R] 
+20
source

You have an L flag that is missing from several rules. Enter the code to change the code:

 Options +FollowSymLinks -MultiViews # Turn mod_rewrite on RewriteEngine On RewriteBase / # Force HTTPS RewriteCond %{HTTPS} !=on RewriteCond %{ENV:BWC_ENV} ^prod$ RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] # Parse the subdomain as a variable we can access in our scripts RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{QUERY_STRING} !^$ RewriteCond %{HTTP_HOST} !^www RewriteCond %{HTTP_HOST} ^([^.]+)\.[^.]+\.[^.]+$ RewriteRule ^(.*)$ /$1?subdomain=%1 [L,QSA] # Ditto for the path; map all requests to /index.php RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !robots.txt RewriteRule ^(.*)$ /index.php?path=$1 [L,QSA] # robots.txt - supply the correct one for each environment RewriteRule ^robots.txt$ /robots.prod.txt [L,NC] RewriteCond %{ENV:BWC_ENV} !prod RewriteRule ^robots.prod.txt$ /robots.stage.txt [NC,L] 
+1
source

Source: https://habr.com/ru/post/1500827/


All Articles