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.

Is it possible to have foo.com and www.foo.com resolve to index.html and admin.foo.com resolve to index.php? Both index.html and index.php reside in the same directory? I am tried the below configuration and all subdomains resolved to index.html.

<VirtualHost *:80>
  ServerName foo.com
  ServerAlias www.foo.com
  DocumentRoot "/var/www/foo"
  DirectoryIndex index.html
</VirtualHost>

<VirtualHost *:80>
  ServerName admin.foo.com
  DocumentRoot "/var/www/foo"
  DirectoryIndex index.php
</VirtualHost>
share|improve this question

3 Answers 3

up vote 4 down vote accepted

According to the Apache documentation, DirectoryIndex can be specified in a VirtualHost. If you have any other DirectoryIndexes specified, you might try removing them and only specifying within the VirtualHost. Your style of configuration would have to support this, however.

As an alternative, you might consider using mod_alias or mod_rewrite instead. Check out the Redirect functions with mod_alias.

share|improve this answer
    
mod_rewrite would work well in this instance. You can set logic such that if the host-header is "www." it'll serve one thing and if it is "admin." it'll serve another, but the rest will be the same. –  sysadmin1138 Jul 15 '10 at 14:19

An other solution is to set only index.php as DirectoryIndex and make a PHP redir into index.php

if ($_SERVER['SERVER_NAME'] != "admin.foo.com") {
  Header('Location: index.html');
}
share|improve this answer

You don't need the overhead of mod_rewrite for something this simple, and that's not an answer to your question, anyway, which is, why doesn't your setup, as listed, work correctly?

The answer to that issue is that DirectoryIndex is cumulative. Therefore, if you have

DirectoryIndex index.html default.htm

above these configurations in your Apache config file, then having:

DirectoryIndex index.php

results in an effective DirectoryIndex of:

DirectoryIndex index.html default.htm index.php

Now, you could shuffle around the order of things in your config file, but a far-more-amusing option would be to add:

<Files /var/www/foo/index.html>
    Deny from all
</Files>

inside your PHP-powered VHost, which, since Apache can't read index.html by policy, will cause it to failover to the next entry in the DI list.

Then again, come to think of it, maybe just re-ordering your config file is a better idea. ;)

share|improve this answer

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.