PhalconPHP + .htaccess:如何强制使用 https
PhalconPHP + .htaccess: how to force https
我在使用 Phalcon PHP 制作的网站中配置了 HTTPS。现在我想将对 HTTP 发出的任何请求重定向到 HTTPS。服务器是 AWS EC2,带有负载均衡器。
Phalcon PHP 有两个 .htaccess 文件:
/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/ [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=/ [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 中的答案,但仍然无效。
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/ [L]
</IfModule>
Nikolay 的回答是正确的,但问题出在其他方面:AWS Load Balancer 的问题。所以,这是我当前的根 .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/ [L]
</IfModule>
亚马逊的文章here。
我在使用 Phalcon PHP 制作的网站中配置了 HTTPS。现在我想将对 HTTP 发出的任何请求重定向到 HTTPS。服务器是 AWS EC2,带有负载均衡器。
Phalcon PHP 有两个 .htaccess 文件:
/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/ [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=/ [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 中的答案,但仍然无效。
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/ [L]
</IfModule>
Nikolay 的回答是正确的,但问题出在其他方面:AWS Load Balancer 的问题。所以,这是我当前的根 .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/ [L]
</IfModule>
亚马逊的文章here。