PDA

View Full Version : ad-hoc subdomains


eddie vanson
18th December 2008, 10:36
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:

<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
RewriteRule (.*) http://example.com/%1 [L]
</IfModule>


This serves the correct page, but redirects the browser from "http://foo.example.com" to "http://example.com/foo".

Any pointers appreciated.

marpada
18th December 2008, 23:58
You need to enable virtual hosts (vhosts) in Apache, there'are lot of info in this site and internet

http://httpd.apache.org/docs/2.0/vhosts/examples.html

You make want to look something for your specific distro (file organization may change)
________
Kitchen Measures (http://kitchenmeasures.com/)
________
DIGITAL VOLCANO (http://vaporizers.net/vaporizers)

eddie vanson
19th December 2008, 03:14
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.

falko
19th December 2008, 17:59
You can do this with mod_rewrite, but I don't have the correct recipe at hand right now...

eddie vanson
22nd December 2008, 01:51
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.

eddie vanson
22nd December 2008, 04:37
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):


/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


<VirtualHost example.com:80>

UseCanonicalName Off

# Admin email, Server Name (domain name) and any aliases
ServerAdmin webmaster@example.com

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.


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.