4

프로덕션 환경에서 HTTPS를 강제하는 규칙을 통합하려고 시도한 후에이 오류가 발생하기 시작했습니다. BWC_ENV 환경 변수가 다른 값의 소수 가질 수 등 "자극", "무대", "ben_local", "nam_local"여기강제 HTTPS 규칙을 추가 할 때 .htaccess 리다이렉트 루프 (Amazon Elastic Beanstalk)

나의 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] 

편집

내 .htaccess에만 다음 내용 만 포함되어 있으면 리디렉션 루프도 발생합니다. 왜 이럴 수 있니?

RewriteEngine On 
RewriteCond %{HTTPS} !=on 
RewriteRule^https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

답변

20

Amazon Elastic Load Balancer의 내용입니다. 이를 위해 Amazon의 X-Forwarded-Proto 헤더를 사용해야합니다.

+1

큰 도움이되었습니다. –

+1

정말 고마워요! 나는 [NearlyFreeSpeech] (https://www.nearlyfreespeech.net/)에서 호스팅되는 사이트를 가지고 있으며, 이것을 작동시키기 위해서'X-Forwarded-Proto '를 사용해야했습니다. 또한'RewriteCond % {HTTPS}! = on'으로 리다이렉트 루프를 만들었습니다. –

+0

@RocketHazmat 잘 알고 있고, 기꺼이 도와 줬습니다! –

1

몇 가지 규칙에서 L 플래그가 누락되었습니다. 코드를 다음과 같이 변경하십시오.

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

내 규칙 @anubhava의 오류를 지적 해 주셔서 감사합니다. 문제의 원인은 실제로 Amazon의 탄력적 인로드 밸런서 였지만 포인터는 다른 이유로 유용했습니다! –

+1

문제가 해결되어서 다행입니다. – anubhava