Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirecting http to https in Google Cloud

I have setup a load balancer which accepts https connections from users. The compute engines are then connected to load balancer using http connection.

I have written the following .htaccess file in the root folder:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Now, the problem is, the compute engine is always connected via http link (to the load balancer) and not https.

Thus, the .htaccess file always consider the connection to be http and not https even if the url starts with https. So, it goes in an infinite loop trying to send the user to https even when the url is https.

What should be done to redirect http to https in this case.

Thanks.

like image 854
Vitul Goyal Avatar asked Jul 05 '16 11:07

Vitul Goyal


Video Answer


1 Answers

You should check the X-Forwarded-Proto http header. It is set by the load balancer and will have the value http or https.

like image 125
Lennert Kuijpers Avatar answered Oct 02 '22 09:10

Lennert Kuijpers