Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have the following virtual host config:

<VirtualHost *>
    ServerName books.domain.com
    ServerAlias ebooks.domain.com
    DocumentRoot /home/media/books/
    <Directory /home/media/books>
    	Options Indexes FollowSymLinks MultiViews
    	AllowOverride None
    	Order allow,deny
    	allow from all
    </Directory>
    ErrorLog /home/domain.com/logs/error.log
    CustomLog /home/domain.com/logs/access.log combine
    ServerSignature Off
</VirtualHost>
<VirtualHost *>
    ServerName domain.com
    ServerAlias domain.info domain.net domain.org *.domain.com *.domain.info *.domain.net *.domain.org
    DocumentRoot /home/domain.com/public_html/
    <Directory /home/domain.com/public_html>
    	Options Indexes FollowSymLinks MultiViews
    	AllowOverride None
    	Order allow,deny
    	allow from all
    </Directory>
    ErrorLog /home/domain.com/logs/error.log
    CustomLog /home/domain.com/logs/access.log combine
    ServerSignature Off
</VirtualHost>

domain.com is the same across both virtual host directives

The problem is that books.domain.com is being served from the /home/domain.com/public_html/ directory

share|improve this question
    
It's hard to tell what's going on without more info... are these two sections directly next to each other in the same config file, as you show them here? Or are there directives that appear between them? Are you using an HTTP/1.1 client that sends the proper Host: header? What happens if you include port numbers in the vhost definitions, like <VirtualHost *:80>? –  David Z Jul 30 '09 at 4:34
add comment

5 Answers

Maybe because you have ServerAlias rule for domain.com with

*.domain.com

which will match ebooks.domain.com and serve from

/home/domain.com/public_html

My best advice would be use mod_rewrite with RewriteCond %{HTTP_HOST} but unfortunately I don't know enough to give you any more specifics. Take a look at mod_rewrite manuals/guides.

share|improve this answer
    
Is there a simple solution to this because I need this sub domain to be served from a separate directory, but I want the wild card? –  Unkwntech Jul 30 '09 at 2:23
    
You could go completely wildcard if you can re-arrange the directory structure like /home/media/books, home/media/ebooks etc then try something like this: DocumentRoot /home/media #make sure you have a default index page RewriteEngine on RewriteCond %{SERVER_NAME} ^(.*).domain.com [NC] # make sure this is a subdomain call RewriteCond /%1/ -d # is there a directory for this subdomain RewriteRule ^/(.*)$ /%1/ # request filename now /home/media/%1 - client URL unchanged. –  DisabledLeopard Jul 30 '09 at 3:20
    
bah, formatting was ruined ... also, I should have used %{HTTP_HOST} not %{SERVER_NAME} ... I'll post as an answer to give formatting. –  DisabledLeopard Jul 30 '09 at 3:25
add comment

I'd have thought that the order was the key here, that apache would serve the first matching servername, but I did a quick test, and that doesn't seem to be the case. It always seems to match the wildcard, no matter what order they're in.

The best I could think of with one ip address is pretty much what Evgeny suggested, have one virtualhost and use a proxy redirect for books.domain.

<VirtualHost *>
    ServerName domain.com
    ServerAlias domain.info domain.net domain.org *.domain.com *.domain.info *.domain.net *.domain.org
    DocumentRoot /home/domain.com/public_html/
    <Directory /home/domain.com/public_html>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>
    ErrorLog /home/domain.com/logs/error.log
    CustomLog /home/domain.com/logs/access.log combine
    ServerSignature Off

    <Directory /home/media/books>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>

    Alias /home/media/books /books
    RewriteRule   ^books\.domain\.com(.*)$      http://domain.com/books$1 [NC,P,L]
    RewriteRule   ^ebooks\.domain\.com(.*)$     http://domain.com/books$1 [NC,P,L]

</VirtualHost>

The only way I can think of to preserve the wildcards and seperate vhosts would be if you had two ip addresses, and assigned the two virtualhosts to seperate addresses. I still feel there must be a better way to do this though, will be interested the other solutions.

share|improve this answer
    
the manual is not clear about it but it seems that pattern cannot include the domain name, it seems to match the path relative to document root. RewriteCond can catch the domain name which you can use in the substitution pattern. –  Evgeny Jul 30 '09 at 16:55
    
you also want to add RewriteEngine on before the rules –  Evgeny Jul 30 '09 at 16:58
add comment

I think Evgeny nailed it.

Good to know: The value of a NameVirtualHost directive has to match the content of exactly and neither should be a hostname. For example, NameVirtualHost *:80 must be used with . "NameVirtualHost *:80" must appear only once in a configuration.

share|improve this answer
add comment

As Evgeny noted books.domain.com is served from public_html as that is exactly what the VirtualHosts say ... your first VirtualHost ServerAlias is ebooks.domain.com to serve from /home/media/books. Make sure the *.domain.com is always in the last VirtualHost [which you have] as long as some preceding VirtualHost has a ServerAlias matching the domain you enter that VirtualHost will serve the response.

If you're looking for a more completely wildcard solution and you can re-arrange the directory structure like /home/media/books, home/media/ebooks etc then try something like this and get away with everything in a single VirtualHost:

DocumentRoot /home/media #make sure you have a default index page 
RewriteEngine on 
RewriteCond %{SERVER_NAME} ^(.*).domain.com [NC] # make sure this is a subdomain call 
RewriteCond /%1/ -d # is there a directory for this subdomain 
RewriteRule ^/(.*)$ /%1/ # request filename now /home/media/%1 - client URL unchanged.
share|improve this answer
add comment

Do you have a "NameVirtualHost *:80" or similar as a global config line?

That's usually what's wrong when virtual hosts just don't work in Apache.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.