#1  
Old 18th December 2008, 09:36
eddie vanson eddie vanson is offline
Junior Member
 
Join Date: Dec 2008
Posts: 4
Thanks: 0
Thanked 1 Time in 1 Post
Default ad-hoc subdomains

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 (.*) 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.
Reply With Quote
Sponsored Links
  #2  
Old 18th December 2008, 22:58
marpada marpada is offline
Senior Member
 
Join Date: Sep 2008
Posts: 139
Thanks: 2
Thanked 14 Times in 14 Posts
Default

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
________
DIGITAL VOLCANO

Last edited by marpada; 13th May 2011 at 01:59.
Reply With Quote
  #3  
Old 19th December 2008, 02:14
eddie vanson eddie vanson is offline
Junior Member
 
Join Date: Dec 2008
Posts: 4
Thanks: 0
Thanked 1 Time in 1 Post
Default

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.
Reply With Quote
  #4  
Old 19th December 2008, 16:59
falko falko is offline
Super Moderator
 
Join Date: Apr 2005
Location: Lüneburg, Germany
Posts: 41,701
Thanks: 1,900
Thanked 2,743 Times in 2,577 Posts
Default

You can do this with mod_rewrite, but I don't have the correct recipe at hand right now...
__________________
Falko
--
Download the ISPConfig 3 Manual! | Check out the ISPConfig 3 Billing Module!

FB: http://www.facebook.com/howtoforge

nginx-Webhosting: Timme Hosting | Follow me on:
Reply With Quote
  #5  
Old 22nd December 2008, 00:51
eddie vanson eddie vanson is offline
Junior Member
 
Join Date: Dec 2008
Posts: 4
Thanks: 0
Thanked 1 Time in 1 Post
Default

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.
Reply With Quote
  #6  
Old 22nd December 2008, 03:37
eddie vanson eddie vanson is offline
Junior Member
 
Join Date: Dec 2008
Posts: 4
Thanks: 0
Thanked 1 Time in 1 Post
 
Default

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 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.

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.
Reply With Quote
The Following User Says Thank You to eddie vanson For This Useful Post:
falko (22nd December 2008)
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
SSL for multiple subdomains chillifire Installation/Configuration 12 16th April 2008 11:01
Domains and Subdomains braakiss Installation/Configuration 8 16th August 2007 14:27
customer's subdomains t-mug Installation/Configuration 5 16th August 2007 14:22
domains, subdomains, webspace, bandwidth.. TheRudy General 17 19th February 2007 18:31
subdomains and ispconfig thesquib Installation/Configuration 4 1st December 2006 15:24


All times are GMT +2. The time now is 01:39.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.