If you have NameVirtualHost on, you have to use the IP. NameVirtualHost is needed if you are running SSL, or running VirtualHosts on different IP addresses.
<VirtualHost 172.16.4.1:80>
DocumentRoot /var/www/html/secure
ServerName secure.com
Redirect / https://secure.com
</VirtualHost>
<VirtualHost 17.16.4.1:80>
DocumentRoot /var/www/html/notsecure
ServerName notsecure.com
</VirtualHost>
To be fairly honest with you, the best approch i can see for this would be if you setup your main domain normally like this:
<VirtualHost *:80>
ServerName www.example.com
DocumentRoot /www/domain
</virtualhost>
Then you create a new virtualhost that will hold all domains you want to redirect like this:
<VirtualHost *:80>
ServerName example.com
ServerAlias foo.example.com bar.example.com others.example.com
DocumentRoot /www/redirect_folder
</virtualhost>
Inside that folder make a simple index.php page that summons the 301 so any domains hold in there will be redirect to your main domain with the 301 code.
<?
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.example.com" );
?>
Why do you think it is better this way ?
This way you won't have to keep updating a bunch of places everytime you have a new domain to hold and redirct to your main domain and it won't be serving your users with the current name but will actually redirect them to your main domain in question.
If you are the server owner you can make it even better, you can put the 2nd virtualhost as the first virtualhost in your httpd.conf of vhost.conf file and whenever you hit the IP of your server it will lead you to the redirection page which will lead your users to the main domain in this case instead of having to set a bunch of ServerAlias you can just create the DNS A record for that given subdomain or domain leading to your IP and the server will take care of the rest.
In this last case all you would need for your virtual host would be:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /www/redirect_folder
</virtualhost>
as you dont need the ServerAlias since every and each request that hits your server IP will go to your first vhost.
In addition if you wanted to do this using .htaccess, it would be something like this i belive:
RewriteEngine on
rewritecond %{http_host} ^foo.example.com [nc]
rewriterule ^(.*)$ http://www.example.com/$1 [r=301,nc]
Best Answer
Is there a way to set up VirtualDocumentRoot based on a conditional
?No -- That's simply not how
VirtualDocumentRoot
works -- It picks apart a portion of the hostname and sticks it into a path.or is there a way to use RewriteRule/Cond to detect that '/' is the incorrect folder if there is a drupal folder or a public folder?
I'm not 100% sure what you're asking here, but it sounds like "Is there a way to make Apache behave differently based on the contents of a directory?" - If so the answer is pretty much "No":
Apache just serves what it's given. It has know way of "knowing" if a directory is a Drupal site (It just sees a PHP file, and fires up a PHP interpreter because that's what it's been told to do with PHP files, and spits out the results the interpreter gives it back).
You seem to be trying to shove the square peg of your radically different sites into the round hole of
VirtualDocumentRoot
(which is really best suited to dead simple sites) -- In your case I think you really need to create separate virtual hosts for each site and manage their configurations separately.