【问题标题】:PhalconPHP + .htaccess: how to force httpsPhalconPHP + .htaccess:如何强制 https
【发布时间】:2017-05-24 02:51:31
【问题描述】:

我在一个使用 Phalcon PHP 制作的网站中配置了 HTTPS。现在我想将对 HTTP 的任何请求重定向到 HTTPS。服务器是 AWS EC2,带有负载均衡器。

Phalcon PHP 有两个 .htaccess 文件:

/.htaccess

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule  ^$ public/    [L]
    RewriteRule  (.*) public/$1 [L]
</IfModule>

/public .htaccess

AddDefaultCharset UTF-8

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</IfModule>

我已按照 this post 上的说明将其添加到这些文件中,我得到了 ERR_TOO_MANY_REDIRECTS 。

# force HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L] 

你能帮我弄清楚我在这里做错了什么吗?
感谢您的帮助。

更新: 我猜这是 AWS 的负载均衡器的问题。这是我的配置:一个带有负载均衡器的 EC2 实例(使用 SSL 证书),然后在我的 Route53 中我指向这个负载均衡器。我试过this post的答案,还是不行。

【问题讨论】:

    标签: .htaccess amazon-web-services mod-rewrite phalcon elastic-load-balancer


    【解决方案1】:

    HTTPS 重定向或任何其他重定向应位于 .htaccess 文件中的 Phalcon 规则之前。

    这是我在 root 文件夹中的 .htaccess 文件:

    <IfModule mod_rewrite.c>
        RewriteEngine On
    
        # Force HTTPS
        RewriteCond %{HTTPS} off
        RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    
        # Force www
        RewriteCond %{HTTP_HOST} !^$
        RewriteCond %{HTTP_HOST} !^www\. [NC]
        RewriteCond %{HTTPS}s ^on(s)|
        RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    
        # Forward to /public/ folder
        RewriteRule ^(.*)$ public/$1 [L]
    </IfModule>
    

    【讨论】:

    • 感谢您的回答。您的 .htaccess 应该可以工作,但由于某种原因它不能在我的服务器上工作。您的代码在 htaccess.mwl.be 上通过了测试,但在我的服务器上却出现 503 错误。我正在尝试解决这个问题。找到问题我会在这里写,一定是一个小细节
    【解决方案2】:

    Nikolay 的回答是正确的,但问题出在其他方面:AWS 负载均衡器的问题。所以,这是我当前的根 .htaccess:

    <IfModule mod_rewrite.c>
            RewriteEngine on
    
            #solves the problem with load balancer
            RewriteCond %{HTTP:X-Forwarded-Proto} =http
            RewriteRule ^$ https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
    
            RewriteCond %{HTTP:X-Forwarded-Proto} =http
            RewriteRule . https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
    
            RewriteRule  ^$ public/    [L]
            RewriteRule  (.*) public/$1 [L]
    </IfModule>
    

    亚马逊的文章here

    【讨论】:

      猜你喜欢
      • 2012-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-02
      • 2019-12-17
      • 2016-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多