使用AWS ELB将所有http流量重新路由到https

时间:2014-05-17 00:00:07

标签: apache mod-rewrite amazon-web-services amazon-elb

所以我查看了其他类似的问题并提供了解决方案,但似乎没有一个因为某种原因而起作用。所以,首先,我的ELB设置为

HTTP (incoming) -> HTTP (instance)
HTTPS (incoming) -> HTTP (instance)

因此,两个流量都应该在端口80上进行。这就行了,就像我使用http://mydomain.comhttps://mydomain.com访问我的网站一样,即使我只有一个VirtualHost,它也可以显示80号港口。

问题在于尝试将所有http流量重写为https。我用它来做基于端口的(检查是否!443并重写为https)但是现在一切都进入了80.所以我运行Apache服务器并且有这个重写规则< / p>

RewriteEngine on
RewriteCond %{HTTP_HOST} www.(.+) [OR,NC]    # Added
RewriteCond %{HTTP:X-Forwarded-Proto} !https [NC]
RewriteRule ^/?(.*) https://mydomain.com%{REQUEST_URI} [L,R=301]

但它似乎永远不会起作用。我还缺少其他线路吗?有没有办法检查它是否达到了这个条件?我尝试了!https http 作为条件,但都没有效果。

编辑:稍微将我的RewriteRule改为现在的状态,但它仍无法正常工作。我添加了一个额外的条件来重写www,工作。 HTTP:X-Forwarded-Proto要么没有,要么没有由负载均衡器设置

编辑:错误真的很愚蠢。我只是简单地连接到错误的实例。谢谢你忍受我的愚蠢

2 个答案:

答案 0 :(得分:6)

要从http重写为https,请使用以下规则。
还要检查你的mod重写是否已启用并正常工作。

RewriteEngine On
# This will enable the Rewrite capabilities

RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS for "normal" conditions

RewriteCond %{HTTP:X-Forwarded-Proto} !https
# This checks the connection is not already HTTPS for AWS conditions

RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
# This rule will redirect users from their original location, to the same location but using HTTPS.
# i.e.  http://www.example.com/foo/ to https://www.example.com/foo/
# The leading slash is made optional so that this will work either in httpd.conf
# or .htaccess context

答案 1 :(得分:4)

只是你的RewriteRule无效。请参阅this post了解它的外观。