1

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

Kevin Brown
  • 263
  • 2
  • 13
  • 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? –  Jun 08 '11 at 18:24
  • possible duplicate of [Subdomains from folders on Apache](http://serverfault.com/questions/246652/subdomains-from-folders-on-apache) – Shane Madden Jun 08 '11 at 18:25

1 Answers1

1

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
Stefan Lasiewski
  • 23,667
  • 41
  • 132
  • 186
  • Where do I include this? Again, new to server stuff...is it httpd.conf or apache2.conf or a .htaccess file? – Kevin Brown Jun 08 '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 08 '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 08 '11 at 19:05
  • 1
    I did `sudo a2enmod vhost_ailis` and restart worked! – Kevin Brown Jun 08 '11 at 19:07
  • Can I do something like `subdomain.20.34.56.67`? or must it be my actual named URL? – Kevin Brown Jun 08 '11 at 19:10
  • @Kevin There are variables available besides just '%0', and you can use that to do some creative things like '$hostname-$ipaddress'. However, I can't find a list of the variables just now. – Stefan Lasiewski Jun 08 '11 at 20:12
  • I'd be interested in any updates--this stuff is still outside my scope of knowledge! – Kevin Brown Jun 08 '11 at 21:36
  • This is working very well for domains `example.com`, but if I want a subdomain `something.example.com` I'm having trouble. Are there any problems that could be caused by this? – Kevin Brown Jun 08 '11 at 21:46
  • Does `/var/www/something.example.com/` exist? – Stefan Lasiewski Jun 09 '11 at 00:08
  • Indeed it does... – Kevin Brown Jun 09 '11 at 02:53
  • Here's what I've discovered thus far: I can manually add virtualhosts to my httpd.conf and it works fine. It isn't recgonizing anything else, though--it was recgonicing domain1.com from my `sites-available` folder that I had previously set up. I'm making headway, though! Thanks! – Kevin Brown Jun 09 '11 at 03:25