问题
I have created https load balance and added ssl certificate. site working with https:// but its not working http:// and getting 404 error
Added Headername as X-Forwarded-Proto and avlue as https in load balace header request
Added in .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
is there any to redirect http to https to avoid 404 error?
回答1:
At this time, there is no way to directly configure the GCP Load Balancer to redirect traffic from HTTP to HTTPS; however, there is a workaround to do this. Using Nginx, you can add the following string within the nginx configuration file:
if ($http_x_forwarded_proto = "http") {
return 301 https://$host$request_uri;
}
If you are using Apache, then you will have to do the following if you want to redirect the traffic using the .htaccess file:
- Run either "sudo a2enmod rewrite" or "LoadModule rewrite_module modules/mod_rewrite.so" depending on the Linux OS you are running. This will enable
Edit or create the .htaccess file in the domain root directory with the following:
RewriteEngine On RewriteCond %{HTTPS} !=on RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
You can find more details on the following link here.
There is also an internal request to have this feature added within GCP HTTP/HTTPS Load Balancers. I cannot provide an ETA on if/when it will be applied; however, you can follow the Public Issue Tracker (PIT) on the progress of the request.
来源:https://stackoverflow.com/questions/58246708/redirect-http-to-https-google-cloud-load-balance