Guys i have litle problem with migrating site from HTTP to HTTPS.
I want to force all to www https
http://example.com
->https://www.example.com
(wrok)http://www.example.com
->https.www.example.com
(not work)
Above example return url with two www.www
https.www.www.example.com
My code
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
What i do wrong?
CodePudding user response:
You can use this redirect to combine http -> https
and www
redirects into one rule:
# add www and turn on https in a single redirect rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(. )$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=302,L,NE]
Once you verify it is working fine, replace R=302
to R=301
. Avoid using R=301
(Permanent Redirect) while testing your mod_rewrite
rules.
Your rule on the other hand is attempting to maintain same scheme (http or https) after redirect. Your rule is also working for the case when www
is missing. You need to use OR
between 2 conditions as I have shown in my answer.