1

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.

Community
  • 1
  • 1
André Luiz
  • 6,642
  • 9
  • 55
  • 105

2 Answers2

1

The HTTPS redirect or any other redirects should go before the Phalcon rule in your .htaccess file.

This is my .htaccess file in the root folder:

<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>
Nikolay Mihaylov
  • 3,868
  • 8
  • 27
  • 32
  • Thanks for answering. Your .htaccess should work but it is not working on my server for some reason. You code passes the test on http://htaccess.mwl.be/ but on my server it gets me a 503 error. I'm trying somethings to fix this. I will write back here when I find the issue, it must be a small detail – André Luiz Jan 09 '17 at 20:13
1

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.

André Luiz
  • 6,642
  • 9
  • 55
  • 105