Question

Naked domain redirect to https with www

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.

Show comments

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.

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:

  1. 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:

  1. sudo a2enmod rewrite

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

  1. 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;
}

Hi @xMudrii @ayan, I too want to do this, but somehow, I can’t figure out the default 301 redirection to example.com.

So, I have setup the LetsEncrypt SSL and it’s working fine, but instead of redirecting to www.example.com, all the redirection is pointing on example.com. and I am unable to figure it out.

I added your code, but that creates the redirection loop.

Edit (Answer) : I did fixed it from Wordpress backend, but updating the www in site url and wordpress url.

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.

Try DigitalOcean for free

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

Sign up

Become a contributor for community

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

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.