way to redirect page to www using virtual host configuration in sites-available for apache2?

Solution 1:

Use:

SeverName example.com
ServerAlias www.example.com

in your virtual host configuration...

Then you can redirect non-www to www with something like:

<IfModule mod_rewrite.c>
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
  RewriteCond %{HTTP_HOST} (.+)$ [NC]
  RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
</IfModule>