Setting up wildcard subdomain on ubuntu 14.04 with a2ensite

apache-2.2ubuntu-14.04wildcard-subdomain

I am using apache on ubuntu 14.04. I set up a wildcard domain for my site but it isn't working how I would like it to. I want

www.example.com --> /var/www/mysite
example.com --> /var/www/mysite

and everything else to go to

*.example.com --> /var/www/myothersite

BUT, Right now it is working like this

example.com --> /var/www/mysite

and

www.example.com --> /var/www/myothersite
*.example.com --> /var/www/myothersite

I created the virtual hosts by creating two conf files in sites-available/

example.com.conf
catchall.example.com.conf

Here is the contents of example.com.conf

ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/mysite

Here is the contents of catchall.example.com.conf

ServerName catchall.example.com
ServerAlias *.example.com
DocumentRoot /var/www/myothersite

After I created the files I ran a2ensite on the files and reloaded apache. Any idea on how I can achieve the results I am looking for while still using sites-available and a2ensite?

Best Answer

I think the problem is that Apache parses Included configuration files in lexicographical order and catchall.example.com.conf is parsed before example.com.conf.

The result is that www.example.com is matched with *.example.com , rather than the explicit ServerAlias in the example.com.conf file.

The solution is to rename catchall.example.com.conf to something like wildcard.example.com.conf that comes later in the alphabet then example.com.conf.

Related Topic