Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to redirect from any direction to our site with HTTPS protocol, but some redirects it's not working. I want this:

  • http://www.site.co TO https://www.site.co
  • http://site.co TO https://www.site.co

This is my htaccess:

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

RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]

The second rule it's not working. It going to another direction inside our site, and it isn't redirect to HTTPS site.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
643 views
Welcome To Ask or Share your Answers For Others

1 Answer

Try it like this:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

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

The only real difference here is that first we redirect from non-WWW to WWW then we check for HTTPS and redirect it.

If it does not work, try this one:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301] 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...