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'm trying to configure dynamic virtual hosts with Apache and vhost_alias but I can't figure out how to do this:

I'd like to set my path like this:

/var/www/vhosts/example.com/www         -> example.com
/var/www/vhosts/example.com/www         -> www.example.com
/var/www/vhosts/example.com/sub         -> sub.example.com
/var/www/vhosts/example.com/sub.foo     -> sub.foo.example.com
/var/www/vhosts/example.com/sub.foo.bar -> sub.foo.bar.example.com

So I configured the Virtual Host like this:

UseCanonicalName Off

<VirtualHost *:80>
  VirtualDocumentRoot /var/www/vhosts/%-2.0.%-1.0/%-3+

%-2.0 gets all the penultimate part (2nd level domain) %-1.0 gets all the last part (Top Level domain) %-3+ gets everithing else before that

Ref. http://httpd.apache.org/docs/2.2/mod/mod_vhost_alias.html

The question is: How do I tell apache to serve me the files into www when asking for the domain without www as 3rd level?

The paths work fine except when I try to reach the website without www

share|improve this question
add comment

Your Answer


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

Browse other questions tagged or ask your own question.