Question

Apache, non-WWW to WWW (HTTPS/Secure) and Let's Encrypt

Okay, so I’ve been able to install a Let’s Encrypt cert on my site, and I want all variations of my domain name to go to https://www.

I’ve been able to redirect the insecure http versions (both www and non-www) to the https://www version, but I cannot figure out how to get the https://[non-www] version to redirect. All I get is an internal 500 error.

Normally, I use the one config file to handle everything, but since Let’s Encrypt generated it’s own config, I’m unsure what to do.

Here is my “normal” config file; basic and just “gets the job done”.

UseCanonicalName On

<VirtualHost *:80>
  ServerName www.domain.co.uk
  ServerAlias domain.co.uk
  DocumentRoot /var/www/html/domain
  Redirect permanent / https://www.domain.co.uk
</VirtualHost>

Here’s my Let’s Encrypt.

<IfModule mod_ssl.c>
  <VirtualHost *:443>
    ServerAdmin admin@domain.co.uk

    ServerName www.domain.co.uk
    ServerAlias domain.co.uk

    DocumentRoot /var/www/html/domain

    <Directory /var/www/html/domain/>
      Options FollowSymLinks
      AllowOverride All
      Require all granted

      # Redirect non-www to www
      RewriteEngine On

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

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    Include /etc/letsencrypt/options-ssl-apache.conf
    SSLCertificateFile /etc/letsencrypt/live/domain.co.uk/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/domain.co.uk/privkey.pem
  </VirtualHost>
</IfModule>

Where I’ve got the "Redirect non-www to www are the few lines where I’ve tried to make the changes.

I’ve been struggling on this for months, and I’m just out of ideas.

I have tried a CNAME record for www, and now I have A Records for @.domain and www.domain.

My htaccess file for the site is simply used for the actual site and files (because WordPress), and I’d rather keep it that way.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
August 12, 2019
Accepted Answer

Hello,

I have a good news for you! The fix is realy easy, all you need to do is to move the RewriteRule just out of the <Directory></Directory> block.

So it would look something like this:

<IfModule mod_ssl.c>
  <VirtualHost *:443>
    ServerAdmin admin@domain.co.uk

    ServerName www.domain.co.uk
    ServerAlias domain.co.uk

    DocumentRoot /var/www/html/domain

    <Directory /var/www/html/domain/>
      Options FollowSymLinks
      AllowOverride All
      Require all granted
    </Directory>

    # Redirect non-www to www
    RewriteEngine On

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

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    Include /etc/letsencrypt/options-ssl-apache.conf
    SSLCertificateFile /etc/letsencrypt/live/domain.co.uk/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/domain.co.uk/privkey.pem
  </VirtualHost>
</IfModule>

I’ve tested this with my site and I can confirm that it definitely works!

As always, make sure to backup your config and to run a config test before restarting Apache.

Hope that this helps! Regards, Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel