0

I want to direct visitors to the https url, so with the below code if a visitor types domain.com he will go to the https://www.domain.com

But if he types www.domain.com he will go to the https://www.www.domain.com

How can I avoid this?

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI}
EnexoOnoma
  • 8,454
  • 18
  • 94
  • 179

1 Answers1

0

This works for me:

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

The first rule redirects all requests to www.example.com if the host does not match (missing www, but also "fetch all subdomains"). The second rule redirects all other requests using HTTP to HTTPS. Of course, you have to change example.com to your domain.

André
  • 477
  • 3
  • 11