[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1637
  • Last Modified:

virtual host subdomains

Hello people,

I have a friend with a dedicated server and hes giving me some space on there, but not too good at doing work and I dont want to push all the time. I have a domain being held by a company which allows be to create/amend subdomains but getting him to sort them out his end is a pain.

Right the question is :- Can you setup virtual servers with subdomains pointing to different directories in a single configuration?

a standard configuration would be something like this.
<VirtualHost www.mydomain.com:*>
DocumentRoot /www/mydomain
ServerName www.mydomain.com
# Other directives here

what I would like to do is:-

have a directory /www/mydomain
with subdirectories www, admin, bob

then have www.mydomain.com point to /www/mydomain/www
admin.mydomain.com point to www/mydomain/admin
bob.mydomain.com point to www/mydomain/bob

All i would then do is create a new directory (eg sub) and create the subdomain and point it to my sites ip address. The configuration would then detect the subdomain and point it to the correct directory.

A single configuration would look something like:-
#address without subdomain would point to www.
<VirtualHost mydomain.com>
DocumentRoot /www/mydomain/www
ServerName www.mydomain.com
# Other directives here
# addresses with subdomains point to subdirectory
<VirtualHost *.mydomain.com>
DocumentRoot /www/mydomain/$subdomain
ServerName $subdomain.mydomain.com
# Other directives here

Is this possible??
1 Solution
Hi WayGood,

Definitely yes.

Take a look at Apache tutorial on VirtualHosting at http://httpd.apache.org/docs-2.0/vhosts/

However, you would need to ensure that all your configured subdomain must be resolvable via DNS.

waygoodAuthor Commented:
I have already scanned through the apache site, but couldnt find a way to use the subdomain as part of the document root and server name settings.

I image something like:-

<VirtualHost *.domain.com:80>
ServerName %S.domain.com
DocumentRoot /www/domain/%S

where %S would be the subdomain name

I've got a feeling it something to do with this page http://httpd.apache.org/docs-2.0/vhosts/mass.html
waygoodAuthor Commented:
I found this :-

<VirtualHost *:80>
   UseCanonicalName off
   VirtualDocumentRoot /www/%0
   VirtualScriptAlias /www/%0/cgi-bin
   ServerSignature EMail

So would this work??

<VirtualHost *.domain.com>
   UseCanonicalName off
   ServerName %0.domain.com
   VirtualDocumentRoot /www/domain/%0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Actually VirtualHost is the right setup for your need, but you don't use it the right way.
Your config gets some strange directives.
In your httpd.conf file, ensure that you have a structure like the following, quite simple to understand

  # don't forget this line
   NameVirtualHost *

  # main one
  <VirtualHost *>
  DocumentRoot /www/mydomain
  ServerName www.mydomain.com
  ServerAlias mydomain.com

  # for joe    
  <VirtualHost *>
  DocumentRoot /www/mydomain/joe
   ServerName joe.mydomain.com

NameVirtualHost, <VirtualHost > are expecting an address after their name, put * for the virtual hosting to work with any of the addresses of your server.
Since you stay on port 80 for everybody no need to overcharge the config.
waygoodAuthor Commented:
That nothing to do with my question.

So if I add the subdomain of 'bob' how would this point to /www/mydomain/bob WITHOUT adding another virtual host?????
I have to agree not understanding what you want... :)
If what you need is to redirect to another site for a given URL (with bob...) you better have to use mod_rewrite.
So, if it is the case, have a look to a thread just started about mod_rewrite:

You could have for instance  http://bob.marley.com => http://whatever.net/bob  (whatever and marley could be the same)
waygoodAuthor Commented:
nope, just simple subdomains

www.domain.com points to DIRECTORY www/domain/www
admin.domain.com ponts to DIRECTORY www/domain/admin

BUT instead of having multiple <virtualhost> each with its own ServerName and DocumentRoot I want just 1 as they would all be the same exept for the document root.

so the rule is   *.domain.com points to www/domain/*   where * is any subdomain (eg www or admin in the above)
There should be some secret reason not to use more than 1 virtual host :)
Anyway, I still think you should use mod_rewrite:

RewriteEngine On
RewriteRule  ^http://([^w].*)\.mydomain.com/(.*)$   http://www.mydomain.com/$1/$2  [L]

will make
   http://anything.mydomain.com/rest-of-url  => http://www.mydomain.com/anything/rest-of-url

'anything' not starting with 'w' (avoiding www)
waygoodAuthor Commented:
QUOTE:I have a friend with a dedicated server and hes giving me some space on there, but (he's)not too good at doing work and I dont want to push all the time. I have a domain being held by a company which allows be to create/amend subdomains but getting him to sort them out his end is a pain.

DIRECTORY as in DocumentRoot not webpage. QUOTE: each with its own ServerName and DocumentRoot

SO http://www.domain.com has a document root of   /www/domain/www
http://admin.domain.com has a document root of   /www/domain/admin
http://bob.domain.com has a document root of   /www/domain/bob

see the pattern? #name of subdomain#.domain.com having a docroot of /www/domain/#name of subdomain#
example httpd.conf
<VirtualHost>#change to your server IP
#mod_rewrite rules
UseCanonicalName off
VirtualDocumentRoot /www/htdocs/%1
#additional config stuff here
transforms bob.yourdomain.com to /www/htdocs/bob
and         www.yourdomain.com to /www/htdocs/www
you should rewrite it using mod_rewrite to a valid path so that nobody can do
something like this:

points to /www/htdocs/../../etc/passwd = /etc/passwd

or something similar (I know this shouldn't work becouse of user restrictions
an some other reasons - but never trust someone or something ;-) )

it's also a problem that www.bob.yourdomain.com also points to /www/htdocs/www

so - you should mod_rewrite it BEFORE the rule is used

for more informations and the %1 server variable:


there you also can find some usable stuff about mod_rewrite

good luck

No comment has been added to this question in more than 21 days, so it is now classified as abandoned..
I will leave the following recommendation for this question in the Cleanup topic area:

    ACCEPT: aarfy

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

EE Cleanup Volunteer
I have one particular setup I want to ask for your help
Currently I use VmWare for different hosts.  I also use Kerio Control on a dual connection to the internet, each with a single IP that has a public address.  This is for load balancing and failover.   I am also using DynDNS to have my domain name resolved.  However, every single VmWare appliance has to have either one single IP or use port forwarding.
I'm not a huge fan of portforwarding.  How can I map something like this
domain.com -->
sub1.domain.com --> internal ip
sub2.domain.com --> internal ip
sub3.domain.com --> internal ip
is my question clear?

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now