If both www and non www domain of your website ( and serve the same website, and you want people going to to automatically be redirected to, there's a few way that you can do so with Apache

Redirect non-www URL to www in Apache using htaccess

First of all, make sure mod_rewrite is installed and enabled for your Apache installation. Once it is, add a file named .htaccess in your website's root directory and add the following lines;

RewriteEngine On
RewriteCond %{HTTP_HOST} ^!$ [NC]
RewriteRule ^(.*)$$1 [R=301,L]

Redirect non-www URL to www in Apache using Virtualhost

You can also change your website's Virtualhost setting for the redirect.

<VirtualHost *:80>
    Redirect permanent /

Restart Apache for the changes to take effect

Guide compatibility:

Operating System
Ubuntu 16.04 LTS (Xenial Xerus)
Ubuntu 16.10 (Yakkety Yak)
Ubuntu 17.04 (Zesty Zapus)
Ubuntu 17.10 (Artful Aardvark)
Ubuntu 18.04 LTS (Bionic Beaver)
Ubuntu 18.10 (Cosmic Cuttlefish)
Ubuntu 19.04 (Disco Dingo)