Reputation: 61
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.
Upvotes: 3
Views: 829
Reputation: 46249
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]
Upvotes: 3