user1482261
user1482261

Reputation: 191

Redirect .htaccess to ssl https

I'm trying to redirect both www.site.com and site.com to https://site.com

My code

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

Somehow it's not working properly. How to make it work, and redirect ho https not only from homepages as I wrote above, but from site.com/page and www.site.com/page too?

Upvotes: 0

Views: 160

Answers (3)

jfroom
jfroom

Reputation: 660

The following seems more stable in Chrome and Firefox. It doesn't cause the 'redirection loop' issues.

RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ "https\:\/\/YOURDOMAIN\.com\/$1" [R=301,L]

Upvotes: 0

kinghfb
kinghfb

Reputation: 1021

This is what we were using:

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

Redirects anything with www out the front to the secure equivalent.

Edit: Updated to include the optional www

Upvotes: 2

Jon Lin
Jon Lin

Reputation: 143846

You need to make sure you're not already in HTTPS:

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

And you don't want to match against the host to be site.com AND www.site.com, since it can't be both at the same time.

Upvotes: 1

Related Questions