I know this is a question that is asked several time. I tried several rules but redirection of https://example.com
to https://www.example.com
is not working.
My current redirection rule in the Apache vHost of non SSL is pasted below
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE
The above rule works fine for http://example.com
and http://www.example.com
2
Answers
I find it. This needs to be added in the ssl vhost file.
You already have a solution, but as you have found, the above redirect will obviously only apply to HTTP requests when in the "vhost of non ssl". In this case, the
HTTPS
server variable is always "off" – so the firstRewriteCond
directive is entirely redundant.However, you don’t need mod_rewrite at all when redirecting from HTTP to HTTPS in the HTTP-virtualhost. A simple mod_alias
Redirect
will do the job much "better":