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.

Here is my current setup:

At the bottom of httpd.conf I have the following:

NameVirtualHost *:80
Include /etc/httpd/conf/vhosts/*


Inside of /etc/httpd/conf/vhosts I have the following files:

mainsite.com.conf:

<VirtualHost *:80>
  DocumentRoot "/var/www/vhosts/mainsite.com"
  ServerName mainsite.com
  ServerAlias www.mainsite.com
  <Directory "/var/www/vhosts/mainsite.com">
    allow from all
    Options -Indexes
  </Directory>
</VirtualHost>

sub1.mainsite.com.conf:

<VirtualHost *:80>
  DocumentRoot /var/www/vhosts/sub1.mainsite.com
  ServerName sub1.mainsite.com
  <Directory "/var/www/vhosts/sub1.mainsite.com">
    allow from all
    Options -Indexes
  </Directory>
</VirtualHost>


So far so good! mainsite.com and www.mainsite.com both share the same DocumentRoot (and with some redirection later I can redirect all www.mainsite.com users to mainsite.com for SEO). sub1.mainsite.com also has its own DocumentRoot and doesn't interfere with anything else.

However, <Anything that isn't www or sub1>.mainsite.com gets the same DocumentRoot that mainsite.com and www.mainsite.com get. What I would prefer instead is an error page to come up for any host that doesn't match a VirtualHost.


And now for the actual question: What can I tweak to get the desired effect? Also, can the directory rules that are common between the VirtualHosts be refactored up into httpd.conf? If so, would I just do something like the following?

<Directory "/var/www/vhosts">
  allow from all
  Options -Indexes
</Directory>
share|improve this question
add comment

2 Answers

up vote 3 down vote accepted
<VirtualHost *:80>
ServerAlias *.mainsite.com
...
... 
</VirtualHost>

Should catch anything that isn't otherwise defined.

share|improve this answer
    
Just make sure this config is read last so that the other sites aren't effected. –  racyclist Jun 7 '11 at 21:19
add comment

I use a catchall vhost definition that is the last to be read

<VirtualHost *:80>
        ServerName      default
        ServerAlias *
        DocumentRoot /var/www/default
        ErrorDocument 404 /404.html
        <Directory "/var/www/default">
            Options All
            AllowOverride None
            allow from All
        </Directory>
</VirtualHost>

The default index.html file tells the user that there is noting to see at the address supplied. The 404.html file same.

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.