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.

If I have the following Apache configuration and wildcard *. s1.ex.com pointing to the server

<VirtualHost *>
ServerName example.com
DocumentRoot /var/www/example.com
</VirtualHost>

<VirtualHost *>
ServerName example.net
DocumentRoot /home/user2/web/example.net
</VirtualHost>

Is it possible to automatically get the domains example.com.s1.ex.com example.net.s1.ex.com to automatically point to their respective vhost?

Edit: I'm looking for a solution that automatically do the same as if I had added ServerAlias as manually.

share|improve this question

3 Answers 3

You could add a ServerAlias:

<VirtualHost *>
ServerName example.com
ServerAlias example.com.s1.ex.com
DocumentRoot /var/www/example.com
</VirtualHost>

<VirtualHost *>
ServerName example.net
ServerAlias example.net.s1.ex.com
DocumentRoot /home/user2/web/example.net
</VirtualHost>
share|improve this answer
    
Also note that wildcard characters (* and ?) are allowed in the argument(s) to ServerAlias, so you could even do "ServerAlias example.com.*" and "ServerAlias example.net.*". –  Xhantar Oct 28 '10 at 2:12
    
I know of ServerAlias. I'm looking for a solution that automatically do the same as if I had added ServerAlias manually. –  Christian Oct 28 '10 at 10:03
    
Got it. If you have a large number of domains and want it to happen automatically, I'm sure that you can do it with mod_rewrite. Unfortunately, I am not a mod_rewrite expert. –  Skyhawk Oct 28 '10 at 14:13

Are you looking for for mass virtual hosting?

http://httpd.apache.org/docs/current/vhosts/mass.html

http://www.howtoforge.com/how-to-set-up-mass-virtualhosting-with-apache2-mod_rewrite-mod_userdir-mod_suexec-on-centos-5.3

share|improve this answer
    
Maybe. I'm looking for a solution that will automatically add ServerAlias for all VirtualHost in a specific domain (eg. s1.ex.com) and so the DocumentRoot to be anything for the VirtualHost. In practice, it is a temporary domain name while waiting for DNS. –  Christian Oct 28 '10 at 10:09

I think you want mod_vhost_alias.

LoadModule vhost_alias_module /usr/lib/apache2/modules/mod_vhost_alias.so
<VirtualHost *>
  VirtualDocumentRoot /var/www/%-5.0.%-4
</VirtualHost>

example.com.s1.ex.com is now served from /var/www/example.com.

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.