I’m trying to redirect the user to https://www.example.com
, when the user tries http://example.com
, www.example.com
, https://example.com
, http://www.example.com
etc.
For that I use this code in my .htaccess
-file:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*) https://www.%{SERVER_NAME}/$1 [R,L]
The problem with this is that when the user types in www.example.com
or http://www.example.com
, the user will then be redirected to https://www.www.example.com
. In other words, it duplicates the www.
How can I avoid this?
Maybe this will work:
Have you tried:
This uses apache to get the host and doesn’t rely on it for the redirect, and will also keep the requested URI using apache as well. ^^ (without having to use anything actually matches in the
RewriteRule
)