I want apache to automatically recognize subdomains based on the filename that I have placed in /var/www/subdomains/

I'm a complete apache noob.

P.S. I'm using Ubuntu 10.10

  • I think you need to explain what you want a bit more. I'm guessing from the question you mean you have a domain example.com and you want site1.example.com to map to /var/www/subdomains/site1/, site2.example.com to /var/www/subdomains/site2/ etc. Is that correct? – Smudge Jun 8 '11 at 18:24
  • possible duplicate of Subdomains from folders on Apache – Shane Madden Jun 8 '11 at 18:25

This can be done if you follow the instructions for Simple dynamic virtual hosts in the Apache documentation.

Follow a standard convention for your VirtualHosts, like /var/www/www.subdomain.org/docroot/ and /var/www/www.subdomain.org/cgi-bin, and then add something like the following to your HTTP configuration.

Note how the paths below use %0. This will be populated by the server name which is used in the contents of the Host: header in the HTTP request. You must use UseCanonicalName Off for this to work. Also note that if a visitors goes to 'www.example.com' 'example.com', the 'Host: ' is different, therefore '%0' is different, so you'll need symlinks or another method to tell Apache that /var/www/www.example.com/ and /var/www/example.com/ are the same thing.

# get the server name from the Host: header
UseCanonicalName Off

# this log format can be split per-virtual-host based on the first field
LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
CustomLog logs/access_log vcommon

# include the server name in the filenames used to satisfy requests
VirtualDocumentRoot /var/www/%0/docs
VirtualScriptAlias  /var/www/%0/cgi-bin
  • Where do I include this? Again, new to server stuff...is it httpd.conf or apache2.conf or a .htaccess file? – Kevin Brown Jun 8 '11 at 18:52
  • @Kevin : For testing, put this in httpd.conf. Later on, you can find a more suitable location for this under the conf.d/ directory. I don't have a working Ubuntu box in front of me at the moment, but if you grep for %0 under /etc/apache2/*/*, you might find some great examples for this. – Stefan Lasiewski Jun 8 '11 at 18:57
  • When I try to restart apache I get Invalid command 'VirtualDocumentRoot', perhaps misspelled or defined by a module not included in the server configuration. Do I need to add a module? – Kevin Brown Jun 8 '11 at 19:05
  • 1
    I did sudo a2enmod vhost_ailis and restart worked! – Kevin Brown Jun 8 '11 at 19:07
  • Can I do something like subdomain. or must it be my actual named URL? – Kevin Brown Jun 8 '11 at 19:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.