I'm trying to achieve a redirect from example.com
to www.example.com
(non-www to www).
My sites-available/example.com.conf
file looks like this;
<VirtualHost *:80 *:443>
ServerName www.example.com
ServerAdmin unidan@example.com
DocumentRoot /var/www/example.com/public_html
ErrorLog /var/www/example.com/logs/error.log
CustomLog /var/www/example.com/logs/access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName example.com
Redirect permanent / http://www.example.com
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
Redirect permanent / https://www.example.com
</VirtualHost>
My sites-available/000-default.conf
file looks like this;
<VirtualHost *:80>
ServerAdmin zuckerberg@facebook.com
DocumentRoot /var/www/default/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Now.. when requesting http://www.example.com
it will respond as requested. However, when requesting http://example.com
it will render the request using the configuration in 000-default.conf
.
Unlinking 000-default.conf
from sites-enabled/
will fix this issue, however this is not something I want to do (I want to keep a default behaviour for unknown requests).
What is the issue here? Why does the configuration in 000-default.conf
override the redirect?