Search code examples
.htaccessamazon-web-servicesmod-rewritephalconelastic-load-balancer

PhalconPHP + .htaccess: how to force https


I have configured HTTPS in a website made with Phalcon PHP. Now I want to redirect any request made to the HTTP to HTTPS. The server is an AWS EC2, with load balancer.

Phalcon PHP has two .htaccess files:

/ .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>

I have followed the instructions on this post and added this to these files and I get ERR_TOO_MANY_REDIRECTS .

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

Can you help me figure out what I'm doing wrong here?
Thanks for any help.

UPDATE: I guess it is a problem with the load balancer from AWS. This is my Configuration: An EC2 instance with a load balancer (using the SSL Certificate), then in my Route53 I point to this load balancer. I tried the answers in this post and still doesn't work.


Solution

  • Nikolay's answer is right but the problem was something else: a problem with AWS Load Balancer. So, this is my current root .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>
    

    Amazon's article here.