ayan
By:
ayan

Naked domain redirect to https with www

November 12, 2016 178 views
Apache Let's Encrypt WordPress Ubuntu 16.04

I want to redirect my naked domain to https with www
like => https://www.example.com

I am using Ubuntu 16.04, apache, Let's Encrypt.

1 comment
  • i also have the same question but for Nginx. and dont want 2 rewrite rules. one single rule.

2 Answers
xMudrii November 12, 2016
Accepted Answer

Rewrite rules will help you with this. It's very powerful utility, you do almost all URL manipulations with it.

We will added needed rules to Apache config. If you use default configuration, it's stored in file called 000-default-le-ssl.conf. Open it with text editor:

  • sudo nano /etc/apache2/sites-available/000-default-le-ssl.conf

By default, this is content of file:

GNU nano - /etc/apache2/sites-available/000-default-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

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

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
ServerName example.com
ServerAlias example.com
</VirtualHost>
</IfModule>

You need to add following rules:

Rewrite rules
RewriteEngine On RewriteCond %{HTTP_HOST} !^www\.example\.com [NC] RewriteRule ^/(.*) https://www.example.com/$1 [L,R]

First rule - RewriteEngine On, enables rewrite engine for you.
Second one is condition, if it's you are not accessing via www.example.com, RewriteRule will execute.
Third one is rule, it rewrites your URL to correct one - www.example.com

When you add it, your file will look like:

GNU nano - /etc/apache2/sites-available/000-default-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

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

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

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
ServerName example.com
ServerAlias example.com
</VirtualHost>
</IfModule>

It requires you to have rewrite module enabled. To make sure, execute this:

  • sudo a2enmod rewrite

To make this changes in effect - you'll have to restart Apache:

  • sudo systemctl restart apache2

This can be done via .htaccess to, but as you have access to config files, you can use config files. :)

This is done for @ayan, now let's go on @newbie part. =)
This depends on your setup. If you followed How To Secure Nginx with Let's Encrypt on Ubuntu 16.04, you have 2 server blocks. One to redirect, and one for HTTPS.
In that case, you just need to add www to return. Let's assume you have redirect block like this one:

/etc/nginx/sites-available/default
server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name example.com www.example.com;
    return 301 https://$server_name$request_uri;
}

If you change https://$server_name$request_uri; to https://www.$server_name$request_uri;, you'll get desired result.
And block will look as this:

/etc/nginx/sites-available/default
server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name example.com www.example.com;
    return 301 https://www.$server_name$request_uri;
}
In this tutorial, we will show you how to use Let's Encrypt to obtain a free SSL certificate and use it with Nginx on Ubuntu 16.04. We will also show you how to automatically renew your SSL certificate. If you're running a different web server, simply follow your web server's documentation to learn how to use the certificate with your setup.

Thanks... It's works but when url www.example.com it not redirect "https://www.example.com" please tell me how i will do it.

Have another answer? Share your knowledge.