Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
765 views
in Technique[技术] by (71.8m points)

apache - Rerouting all http traffic to https with AWS ELB

So I've looked over the other similar questions and they offer solutions but none of them seem to work for some reason. So, for starters, my ELB is set up so that

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

So both traffic should come in on port 80. And this works, as when I access my site using http://mydomain.com or https://mydomain.com, it is able to display even though I only have a VirtualHost for on port 80.

The issue is with attempting to rewrite all http traffic to https. I use to do it based on ports (check if !443 and rewrite to https) but that won't work now that everything is going into 80. So I'm running an Apache server and have this rewrite rule

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]

But it never seems to work. Are there other lines I'm missing? Is there a way to check that it's hitting that condition? I tried both !https and http as the condition and neither worked.

edit: Slightly changed my RewriteRule to what it is now and it's still not working. I added an extra condition to rewrite www and that works. HTTP:X-Forwarded-Proto either isn't there or isn't set by the load balancer

edit: The mistake was REALLY dumb. I was simply SSHing into the wrong instance. Thanks for putting up with my foolishness

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To rewrite from http to https use following rules.
Also check if your mod rewrite is enabled and working properly.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...