0

I recently set up a new website running off Wordpress which is working nicely. Since I wanted to have an SSL certificate for the website I thought I'd use Let's Encrypt since I've been hearing a lot of good things about it.

Well I have to agree it took 30 seconds to get SSL working on my domain but there is one small problem.

Let's encrypt has set the following rules:

http://domain.com -> https://domain.com (fails)

http://www.domain.com -> https://www.domain.com (works)

The certificate is only for www.domain.com which is annoying. In the past all the SSL certificates I bought included both domain.com and www.domain.com in the SSL certificate. Not sure if I did something wrong when getting the Let's Encrypt certificate but that is the way it is.

All I need to do is change things so that http://domain.com redirects to https://www.domain.com.

But since I'm new with Apache I have no idea how to do it. I have the following redirect rule in my .htaccess file but it doesn't seem to help since Apache seems to redirect before the request hits the rewrite rule in the .htaccess file. Here is the rule:

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

So I'm pretty stuck on this particular problem. The site correctly redirected from domain.com to www.domain.com before I added the SSL certificate so it seems to be something that Let's Encrypt automated install did and I can't find it.

This is the rewrite rule Let's Encrypt added to my Apache configuration file:

RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [L,QSA,R=permanent]

I think it has something to do with the above line but I don't know what.

Any help would be appreciated.

2

You need to create a separate VirtualHost for a non-www domain and set redirect from it like this:

<VirtualHost *:80> ServerName domain.com Redirect 301 / https://www.domain.com </VirtualHost>

Basically, adding the above code to your httpd.conf should solve your problem. Though you might want to consult your OS documentation on ow to add virtual hosts to apache...

2
  • Hmm added a new VirtualHost document and added it to the Apache configuration with a2ensite (I put exactly the information you said) and it still didn't work. It seems to be redirecting from the last rule I put above rather than hitting the second VirtualHost that I've specified. – Cromulent Jan 9 '16 at 21:57
  • Make sure you don't have any ServerAlias domain.com in your first VirtualHost. It should only have ServerName www.domain.com in there – Anubioz Jan 9 '16 at 22:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.