【问题标题】:Redirect all www and http ==>> non www https on AWS Elastic Beanstalk with Load Balancer使用负载均衡器在 AWS Elastic Beanstalk 上重定向所有 www 和 http ==>> 非 www https
【发布时间】:2014-06-18 04:12:36
【问题描述】:

我目前已将所有http 流量重定向到https

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !=https
RewriteRule ^/(.*)$ https://%{SERVER_NAME}/$1 [R=301,L]

以上工作正常。

现在我正在尝试添加重写条件以强制所有 https://www 流量到 https 而没有 www

请注意,这是在 Elastic Load Balancer 后面运行 Apache 的 AWS Elastic Beanstalk

==============================

编辑:

工作代码:

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !=https [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{SERVER_NAME} ^(www\.)?(.*)$ [NC]
RewriteRule ^/?(.*)$ https://%2/$1 [L,R=301]

【问题讨论】:

    标签: apache .htaccess mod-rewrite amazon-web-services amazon-ec2


    【解决方案1】:

    试试这个规则:

    RewriteEngine On
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} ^www\. [NC]
    RewriteCond %{SERVER_NAME} ^(www\.)?(.*)$ [NC]
    RewriteRule ^/?(.*)$ https://%2/$1 [L,R=301]
    

    【讨论】:

    • 感谢您的回复。不幸的是,它无法正常工作,因为所有流量都被重定向到 https://www. 并且服务器/域丢失。我认为这与 AWS 负载均衡器 X-Forward-Proto 有关
    • @WayBehind 哎呀,%1 应该是 %2
    • 再次感谢!不幸的是,这次我们有"This webpage has a redirect loop"server is redirecting the request for this address in a way that will never complete
    • @WayBehind 你确定你没有其他东西正在重定向到某个地方吗?这些规则不会导致循环,除非其他东西重定向回具有“www”或非 SSL
    • @WayBehind 哦,没错,意思是说你应该用你的行替换%{HTTPS} off 部分:%{HTTP:X-Forwarded-Proto} !=https(尽管需要有那个[OR]
    【解决方案2】:

    其实RewriteCond %{HTTP:X-Forwarded-Proto} ^http$ [OR] 会更好,因为!=https 没有通过 ELB 健康检查。所以完整的工作答案是:

    RewriteEngine On
    RewriteCond %{HTTP:X-Forwarded-Proto} ^http$ [OR]
    RewriteCond %{HTTP_HOST} ^www\. [NC]
    RewriteCond %{SERVER_NAME} ^(www\.)?(.*)$ [NC]
    RewriteRule ^/?(.*)$ https://%2/$1 [L,R=301]
    

    【讨论】:

    • 谢谢!我会试一试!
    【解决方案3】:

    还要考虑 ELB 健康检查的使用:

    RewriteEngine On
    RewriteCond %{HTTP:X-Forwarded-Proto} !=https [OR]
    RewriteCond %{HTTP_HOST} ^www\. [NC]
    RewriteCond %{HTTP_USER_AGENT} !ELB-HealthChecker
    RewriteCond %{SERVER_NAME} ^(www\.)?(.*)$ [NC]
    RewriteRule ^/?(.*)$ https://%2/$1 [L,R=301]
    

    【讨论】:

      【解决方案4】:

      仅适用于寻找使用 ElasticBeanstalks 配置文件的完整复制和粘贴解决方案的任何人。

      1. .ebextensions/ 中创建文件,我们称之为01-https.config
      2. 添加以下内容:
      files:
        "/etc/httpd/conf.d/http-redirect.conf":
          mode: "000644"
          owner: root
          group: root
          content: |
            RewriteEngine On
            RewriteCond %{HTTP_HOST} ^www\.(.*)$ [OR,NC]
            RewriteCond %{HTTP:X-Forwarded-Proto} !https
            RewriteCond %{HTTP_USER_AGENT} !ELB-HealthChecker
            RewriteRule (.*) https://example.com%{REQUEST_URI}
      

      这考虑了 ELB 健康检查,适用于以下示例:

      将被重定向到https://example.com

      【讨论】:

        猜你喜欢
        • 2020-10-18
        • 2017-05-30
        • 2018-02-16
        • 2016-11-26
        • 2017-12-01
        • 1970-01-01
        • 2015-04-06
        • 2018-01-21
        • 2018-09-26
        相关资源
        最近更新 更多