0

我在用Phalcon PHP製作的網站中配置了HTTPS。現在我想將任何對HTTP的請求重定向到HTTPS。該服務器是帶有負載平衡器的AWS EC2。PhalconPHP + .htaccess:如何強制https

爾康PHP有兩個.htaccess文件:

/的.htaccess

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

/公衆的.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的負載平衡器的問題。這是我的配置:具有負載均衡器(使用SSL證書)的EC2實例,然後在我的Route53 I中指向此負載均衡器。我嘗試了this post中的答案,但仍然無法正常工作。

回答

1

尼古拉的答案是正確的,但問題是別的東西:與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

+1

有趣的知道,謝謝分享:) –

1

HTTPS重定向或任何其他重定向應該放在.htaccess文件中的Phalcon規則之前。

這是文件夾我的.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> 
+0

感謝您的回答。您的.htaccess應該可以工作,但由於某種原因,它不適用於我的服務器。你的代碼通過http://htaccess.mwl.be/上的測試,但在我的服務器上它得到了我的503錯誤。我正在嘗試解決這個問題。當我發現問題時,我會寫回到這裏,它必須是一個小細節 –