Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect HTTP > HTTPS when port is always 80

Our application needs to redirect HTTP requests to HTTPS. Normally i would use the RewriteCond as below.

RewriteCond %{HTTPS} off

The thing with our current hosting company is that the requested port is always 80. It doens't matter if we send a HTTP or HTTPS request, the $_SERVER['SERVER_PORT'] is always 80.

We can handle this in PHP, but we also want to force files to HTTPS.

So the concrete question. Is there anyway to force HTTPS in .htaccess without using RewriteCond like RewriteCond %{HTTPS} or RewriteCond %{SERVER_PORT}.

like image 415
S.Pols Avatar asked Apr 15 '26 08:04

S.Pols


1 Answers

Your situation is not that uncommon. Try this (X-Forwarded-Proto is de facto standard for identifying the originating protocol of an HTTP request):

RewriteCond %{HTTP:X-Forwarded-Proto} !https [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
like image 129
Dusan Bajic Avatar answered Apr 16 '26 22:04

Dusan Bajic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!