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

    /var/sites/example.com/public/www/...
    /var/sites/example.com/public/foo/...
    /var/sites/example.com/public/bar/...

    My document root is /var/sites/example.com/public/

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

    I have already setup my dns so that any URL of the form *.example.com 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:

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

    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 'example.com' with your real domain name):

    Code:
    /path/to/sites/example.com/public/subdomains/www
    /path/to/sites/example.com/public/subdomains/foo
    /path/to/sites/example.com/public/subdomains/bar
    
    1) Enable mod_vhost_alias

    2) Put this in your virtual host configuration

    Code:
    <VirtualHost example.com:80>
    
      UseCanonicalName Off
    
      # Admin email, Server Name (domain name) and any aliases
      ServerAdmin [email protected]
    
      ServerName  *.example.com
    
      # 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/example.com/public/subdomains/%1
    
      # Custom log file locations
      # Adjust LogLevel as needed
      LogLevel debug
      ErrorLog  /path/to/sites/example.com/log/error.log
      CustomLog /path/to/sites/example.com/log/access.log combined
    
    </VirtualHost>
    
    3) Restart Apache.

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

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

Share This Page