3

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.

2 Answers 2

3

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]
Sign up to request clarification or add additional context in comments.

Comments

1

Accorting to this site (first result on google)

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

should work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.