Ssl – How to redirect all non www and http traffic to www and https with Apache2

apache-2.2mod-rewritessl

In my httpd.conf, I have

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

But that's not working properly. I want the following to redirect to https://www.mysite.com:

How can I accomplish this?

Best Answer

Not sure what's not working properly. But the following will work:

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

RewriteCond %{HTTPS} off
RewriteRule (.*)           https://www.mysite.com/$1

This can be conceived as a duplicate post of many similar posts about rewrite rules on apache but I find this one particularly well worded about the redirect non-SSL so maybe this can be the last post that will need to be answered and people will be able to find that one easier!