Wednesday, January 10, 2018

How to redirect from non-www to www in Apache

In this post I will show you how to redirect from one site to another in apache. Actually, it's pretty easy to do. But let's learn it by example.

Two sites: site.com and www.site.com.

Apache config looks like this /etc/apache2/sites-available/site.conf:
<VirtualHost *:80>
  ServerName site.com
  DocumentRoot /var/www/site
</VirtualHost>
So both sites can be loaded independently, but there is no redirect.