0

I'm using Apache 2.4 on an installation of Ubuntu 14.04, and I'm in the process of configuring Apache "virtualhosts" in order to be able to serve different content for different hostnames.

I'm trying to configure Apache such that when one tries to access the server with the hostname (www.)example1.com, content from /var/www/example1 is served; however, if one tries to access the server with it's IP address, or any other hostname except for (www.)example1.com (for example, via directly accessing it through the IP, or through any other hostname pointing at this IP) content from /var/www/html is served instead.

I have the following configuration files under /etc/apache2/sites-available:

/etc/apache2/sites-available/example1.conf:

<VirtualHost example1.com:80>
    DocumentRoot /var/www/example1
    ServerAlias www.example1.com
    ErrorLog ${APACHE_LOG_DIR}/ex1_error.log
    CustomLog ${APACHE_LOG_DIR}/ex1_access.log combined
</VirtualHost>

/etc/apache2/sites-available/000-default.conf:

<VirtualHost *:80>
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

However, when I try to access the website by direct access or through the other hostnames pointing at this IP, I get the content served from /var/www/example1 and not from /var/www/html.

What could be the issue here, and how can I fix it?

1
  • In addition to the answers below, I seem to have misconfigured the example1.conf file, where it should have been <Virtualhost *:80> followed by ServerName example1.com and ServerAlias www.example1.com.
    – user175627
    Dec 22, 2015 at 13:34

2 Answers 2

2

On 2.4 use _default_:

<VirtualHost _default_:*>
     DocumentRoot /var/www/html
     ErrorLog ${APACHE_LOG_DIR}/error.log
     CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
0

You can use alias * to catch any other request, but it has to be the last virtual host.

This is how you can do it in your case :

Rename & edit /etc/apache2/sites-available/000-example1.conf:

<VirtualHost *:80>
    DocumentRoot /var/www/example1
    ServerName www.example1.com
    ErrorLog ${APACHE_LOG_DIR}/ex1_error.log
    CustomLog ${APACHE_LOG_DIR}/ex1_access.log combined
</VirtualHost>

Rename & edit /etc/apache2/sites-available/001-default.conf:

# /!\ THIS HAS TO BE ON THE LAST POSITION /!\
<VirtualHost *:80 *:443>
ServerName localhost
ServerAlias *
DocumentRoot /var/www/html/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

You could even rename 001-default.conf to 100-default.conf, like that next time you will had a new virtual host like 002-newhost1.conf, 003-newhost2.conf, etc default will stay at the last position, and will ever take "any other" request.

You must log in to answer this question.