简体   繁体   中英

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.

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]

Accorting to this site (first result on google)

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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