【问题标题】:.htaccess redirect loop when trying to add forced HTTPS rule (Amazon Elastic Beanstalk)尝试添加强制 HTTPS 规则时的 .htaccess 重定向循环(Amazon Elastic Beanstalk)
【发布时间】:2013-09-09 22:27:28
【问题描述】:

我在尝试在生产环境中加入强制 HTTPS 的规则后开始收到此错误。 BWC_ENV 环境变量可以有几个不同的值:“prod”、“stage”、“ben_local”、“nam_local”等。

这是我的 .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]

【问题讨论】:

    标签: apache mod-rewrite amazon-ec2 amazon-elastic-beanstalk amazon-elb


    【解决方案1】:

    事实证明这是 Amazon Elastic Load Balancer 的事情。您必须使用 Amazon 的 X-Forwarded-Proto 标头来完成此操作:

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

    【讨论】:

    • 非常感谢!我有一个托管在NearlyFreeSpeech 上的网站,我也需要使用X-Forwarded-Proto 才能让它工作。我还有一个带有RewriteCond %{HTTPS} !=on 的重定向循环。
    • 太棒了!这正是我所需要的,干杯
    【解决方案2】:

    您在几条规则中缺少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] 
    

    【讨论】:

    • 感谢您指出我的规则中的错误@anubhava。问题的原因实际上是 Amazon 的弹性负载均衡器,但您的指针对其他原因很有帮助!
    猜你喜欢
    • 2014-10-13
    • 2016-08-13
    • 1970-01-01
    • 2014-09-29
    • 2015-01-24
    • 2013-02-09
    • 2015-12-26
    • 2013-03-30
    • 2018-05-02
    相关资源
    最近更新 更多