# Apache virtual hosts with subdomains

## Jamesbch

Hello,

I've got some issues with my multiple domains managing with Apache. I've got some simple directory scheme for every domain. To make it simple I separate domains by their name as following: /var/www/domain_name/sub_domain/ for the url: http://sub_domain.domain_name/ for example my main website http://www.james-b.ch/index.php is in /var/www/james-b.ch/www/index.php. I've set up in my /etc/apache2/vhosts.d/01_custom_vhost.conf one entry :

```
<VirtualHost *:80>

        ServerName *.james-b.ch

        ServerAlias james-b.ch

        DocumentRoot /var/www/james-b.ch/

        UseCanonicalName Off

        VirtualDocumentRoot /var/www/%2.0.%3.0/%1/

        RewriteEngine On

        RewriteCond %{HTTP_HOST} ^james-b\.ch

        RewriteRule /(.*) http://www.james-b.ch/$1 [R=301,L]

</VirtualHost>

<VirtualHost *:80>

# other host

</VirtualHost *:80>

<VirtualHost *:80>

# other host 2

</VirtualHost *:80>

#... etc[/quote]

<VirtualHost *:80>

        ServerName *.example.website.com # Should be ignored?

        ServerAlias example.website.com

        DocumentRoot /var/www/

        UseCanonicalName Off

        VirtualDocumentRoot /var/www/%2.0.%3.0/%1/

</VirtualHost *:80>

```

My /etc/apache2/vhosts.d/00_default_vhost.conf is empty. It's working well for regular domains with sub domain without any problem. Except that my rule for the domain above seems to override any other <VirtualHost *:80> rule. I've tried to set up a domain where there isn't any subdomain (because it is already a sub-domain of someone i'm hosting, so I have a special entry for: example.website.com) but the above rule was applies instead of the rule it should have matched in the particular <VirtualHost *:80> tag. Now I don't know why the rule is overridden by the first one (Apache have to match the domain and apply rules in the <VirtualHost *:80> tag right?). From the apache2 error_log it tries to access to: /var/www/website.com. but this don't exist because I'm excepting /var/www/example.website.com/. As a workaround I've set up some symlinks and it works but it's a total mess!

I've tested the DNS with dig and all is right at this point. I need to clean my config files because it's a bit messy now with all these rules by domain. Any idea how to tidy them? Thank you in advance.

----------

## Arkhelion

Hello,

Check that you do have this line in your conf file

NameVirtualHost *:80

To tidy up a bit your default conf file, you can also use Include(s)

For instance I have :

<VirtualHost *:80>

        ServerName      www.example1.com

        Include /etc/apache2/vhosts.d/example1.com.include

</VirtualHost>

<VirtualHost *:80>

        ServerName      www.example2.com

        Include /etc/apache2/vhosts.d/example2.com.include

</VirtualHost>

...etc...

Then I just put the rules in the include file.

If you have a lot of common parameters, for subdomains for instance, you could use :

<VirtualHost *:80>

        ServerName      www.example1.com

        Include /etc/apache2/vhosts.d/subdomains-common.include

        Include /etc/apache2/vhosts.d/example1.com.include

</VirtualHost>

Then you put the subdomains common rules in the first include and specific domain rules in the second include.

----------

## Jamesbch

Hello Arkhelion,

Your tip is greatly appreciable! I've clean up the mess I had in my file.

I have NameVirtualHost which appears in my custom vhost file. It is in the DEFAULT_VHOST part (it is read by Apache because in /etc/conf.d/apache2 the -D specify it). You can see all my config merged here: http://pastebin.com/zLEdcKTL I hope you will find some details I haven't see.

Thank you.

----------

