Solved

virtual host subdomains

Posted on 2004-04-15
13
1,595 Views
Last Modified: 2013-11-15
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
</VirtualHost>

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
</VirtualHost>
# addresses with subdomains point to subdirectory
<VirtualHost *.mydomain.com>
DocumentRoot /www/mydomain/$subdomain
ServerName $subdomain.mydomain.com
# Other directives here
</VirtualHost>

Is this possible??
0
Comment
Question by:waygood
13 Comments
 
LVL 15

Expert Comment

by:samri
ID: 10841050
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.


HTH.
0
 
LVL 9

Author Comment

by:waygood
ID: 10841605
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
</VirtualHost>

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
0
 
LVL 9

Author Comment

by:waygood
ID: 10842338
I found this :-

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

So would this work??

<VirtualHost *.domain.com>
   UseCanonicalName off
   ServerName %0.domain.com
   VirtualDocumentRoot /www/domain/%0
</VirtualHost>
0
 
LVL 10

Expert Comment

by:Mercantilum
ID: 10843544
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
   ...
  </VirtualHost>

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

--------
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.
0
 
LVL 9

Author Comment

by:waygood
ID: 10843639
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?????
0
 
LVL 10

Expert Comment

by:Mercantilum
ID: 10843723
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:
http://www.experts-exchange.com/Web/Web_Servers/Apache/Q_20955665.html

You could have for instance  http://bob.marley.com => http://whatever.net/bob  (whatever and marley could be the same)
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 9

Author Comment

by:waygood
ID: 10843975
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)
0
 
LVL 10

Expert Comment

by:Mercantilum
ID: 10844047
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)
0
 
LVL 9

Author Comment

by:waygood
ID: 10844118
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.

ALSO:
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#
0
 

Accepted Solution

by:
aarfy earned 125 total points
ID: 11060137
example httpd.conf
--snip--
<VirtualHost 192.168.0.1>#change to your server IP
#mod_rewrite rules
UseCanonicalName off
VirtualDocumentRoot /www/htdocs/%1
#additional config stuff here
</VirtualHost>
--snip--
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:
%2e%2e%2f%2e%2e%2fetc.yourdomain.com/passwd

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:

http://httpd.apache.org/docs-2.0/mod/mod_vhost_alias.html

there you also can find some usable stuff about mod_rewrite

good luck

aarfy
0
 
LVL 15

Expert Comment

by:periwinkle
ID: 11761150
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.

Periwinkle
EE Cleanup Volunteer
0
 

Expert Comment

by:benjalamelami
ID: 33974737
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 --> 190.14.240.50
sub1.domain.com --> internal ip 192.168.0.1
sub2.domain.com --> internal ip 192.168.0.2
sub3.domain.com --> internal ip 192.168.0.3
is my question clear?
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

How to fix error ""Failed to validate the vCentre certificate. Either install or verify the certificate by using the vSphere Data Protection Configuration utility" when you are trying to connect to VDP instance from Vcenter.
Workplace bullying has increased with the use of email and social media. Retain evidence of this with email archiving to protect your employees.
This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…
This tutorial will walk an individual through the process of configuring basic necessities in order to use the 2010 version of Data Protection Manager. These include storage, agents, and protection jobs. Launch Data Protection Manager from the deskt…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now