简体   繁体   English

将所有请求重定向到https:// www symfony2谷歌HTTP负载均衡器

[英]redirect all request to https://www symfony2 google HTTP load balancer

I know this question has been asked so many times earlier but I tried every solutions and none is not working for me . 我知道这个问题已经被问过很多次了,但是我尝试了所有解决方案,但是没有一个对我有用。

I am using symfony2 with google cloud hosting and using their http load balancer. 我正在将symfony2与Google云托管一起使用,并使用其http负载均衡器。 My .htaccess has following code 我的.htaccess有以下代码

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [nocase]
RewriteRule ^(.*) https://www.example.com/$1 [last,redirect=301]

My load balancer have 2 forwarding rules, one for http and one for https. 我的负载均衡器有2条转发规则,一条用于http,另一条用于https。

Now, I can redirect all requests to https://www.example.com , but I am still unable to redirect http://www.example.com to the HTTPS url. 现在,我可以将所有请求重定向到https://www.example.com ,但是仍然无法将http://www.example.com重定向到HTTPS URL。

Please let me know where I am getting it wrong, I was using AWS and rackspace earlier and never faced same issue there. 请让我知道我在哪里弄错了,我之前使用过AWS和机架空间,在那儿再也没有遇到过同样的问题。

Try : 尝试:

RewriteEngine On

#redirect http to https
RewriteCond %{HTTPS} ^off$
RewriteRule ^(.*) https://www.example.com/$1 [last,redirect=301]
#add www
RewriteCond %{HTTP_HOST} ^example.com$ [nocase]
RewriteRule ^(.*) https://www.example.com/$1 [last,redirect=301]

The condition above 以上条件

RewriteCond %{HTTPS} ^off$

checks if the orignal scheme is http, redirect it to https. 检查原始方案是否为http,将其重定向到https。

was able to solve it by following method , I know it is not the best one so suggestions are still invited. 可以通过以下方法解决此问题,但我知道这不是最好的方法,因此仍建议您提出建议。

I added following code in app.php 我在app.php中添加了以下代码

 if($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'http' &&  $_SERVER['HTTP_HOST'] == 'www.example.com') {
 header("Location: https://www.example.com".$_SERVER['REQUEST_URI']);exit;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM