Please Help: redirect non-www to www

LetsEncrypt forces “https” by adding these rules to /apache2/sites-enabled/000-default.conf

<VirtualHost *:80>
RewriteCond %{SERVER_NAME} =example.com [OR]
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

Question:
What is the “best practices way” to ALSO have “non-www” redirect to “www” (with the least possible redirects)?

Concerns:
Although http redirects to https just fine, I cannot get https://example.com to redirect to https://www.example.com properly.

Thank you.

1 Like

Hi @Mary

simple answer: That's wrong.

First redirect http -> https without changing the domain name, without a new DNS query.

Then redirect https + not-preferred version to https + preferred version.

You should always switch immediately to the same domain name, but https. Additional redirects may follow.

1 Like

What a confusing answer. “That’s Wrong…” What, exactly, is wrong? The part where both you and I said that a “non-www to www” redirect should be appended? I would appreciate a line or two of code. Thank you.

1 Like

Hi @Mary!

One of the best examples to achieve your goal is on this forum. Have a “read” on this recent thread.

Www needs to be redirected to non www

Of course in this example the redirect goes from www to non-www. If you take a few minutes, the answer you seek is fully documented there.

Hope this helps!
Rip

3 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.