Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change from http to https?

Does anyone can tell me how to change from http to htpps? My domain name have SSL. I used to create file .htaccess in the root, but it is not work at all.

Here is the code of .htaccess

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

Please help, Thank in advance.

like image 641
Sovat Avatar asked Sep 01 '26 03:09

Sovat


1 Answers

For direct access, this seems to be the way to go:

RewriteEngine on

# rewrite to HTTPS
RewriteCond ${HTTPS} !on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

(based on the documentation: http://httpd.apache.org/docs/current/mod/mod_rewrite.html)

But if you're behind a proxy (such as a load balancer), you'll need to use the headers it sends. Here's the code I use for this:

RewriteEngine on

# rewrite to HTTPS
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

It's served me well, and is fairly self-explanatory.

Of course you could combine both to make it more robust, but that's overkill in any real situation where you know how it will be used;

RewriteEngine on

# rewrite to HTTPS
RewriteCond ${HTTPS} !on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
like image 128
Dave Avatar answered Sep 03 '26 18:09

Dave



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!