ad-hoc subdomains

Discussion in 'Server Operation' started by eddie vanson, Dec 18, 2008.

  1. eddie vanson

    eddie vanson New Member

    I would like to enable ad-hoc subdomains on my Apache based server. In other words, I'm after a setup where all I have to do create a new subdomain is create a new directory on the server.

    My desired file structure is like this


    My document root is /var/sites/

    When I enter "" in a browser, I'd like to serve the contents of /var/sites/, but I still want the URL in the browser to read "".

    I have already setup my dns so that any URL of the form * get routed to my server.

    I have tried using mod_rewrite but don't seem to be able to get the voodoo right. The closest I can get is configuring my .htaccess file thusly:

    <IfModule mod_rewrite.c>
       Options +FollowSymLinks
       Options +Indexes
       RewriteEngine On
       RewriteBase /
       RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
       RewriteRule (.*) [url][/url] [L]                                                                        
    This serves the correct page, but redirects the browser from "" to "".

    Any pointers appreciated.
  2. marpada

    marpada New Member

    Last edited: May 13, 2011
  3. eddie vanson

    eddie vanson New Member

    Vhosts are enabled and I am already serving several websites from a single instance of Apache (I'm using name-based virtual hosting).

    The problem I'm trying to solve is getting the server to conditionally serve content out of different directories based on the subdomain portion of the requested URL, without changing the URL in the requestors browser.

    The goal is to allow users to create subdomains by simply creating or uploading a directory (via shell or ftp) in the appropriate directory.
  4. falko

    falko Super Moderator ISPConfig Developer

    You can do this with mod_rewrite, but I don't have the correct recipe at hand right now...
  5. eddie vanson

    eddie vanson New Member

    Thanks, Falko. Yes, everything I've read suggests that what I want is possible with mod_rewrite. I am probably making some small error somewhere.

    If you could dig up the correct recipe I would be very thankful.
  6. eddie vanson

    eddie vanson New Member

    I was able to achieve what I wanted by using mod_vhost_alias instead.

    Assuming you have your directory structured like this (of course, replace '' with your real domain name):

    1) Enable mod_vhost_alias

    2) Put this in your virtual host configuration

      UseCanonicalName Off
      # Admin email, Server Name (domain name) and any aliases
      ServerAdmin [email protected]
      ServerName  *
      # Index file and Document Root (where the public files are located)
      DirectoryIndex index.html
      # This is the key line, I think
      VirtualDocumentRoot /path/to/sites/
      # Custom log file locations
      # Adjust LogLevel as needed
      LogLevel debug
      ErrorLog  /path/to/sites/
      CustomLog /path/to/sites/ combined
    3) Restart Apache.

    One last possible hitch. If a user goes to (note that there is no subdomain in the URL), Apache will try to serve up /path/to/sites/, which unless you created it will not exist. Anyhow, it's common to have serve the same contents as An easy way to get serve the same contents as is to create a link.

    cd /path/to/sites/
    ln -s www example
    Now when a user asks for, Apache will follow the link named 'example' to www and serve that up.

Share This Page