Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a fairly noob question about how subdomains work.

As I understand at first the DNS server specifies that a request for certain subdomain.domain.com has to go to the IP address of domain.com, and the webserver at domain.com further processes the request and displays the needed subdomain page.

It is not entirely clear to me how (for example Apache) server does it. As I understand there can be entries in vhosts.conf file which specify folders that contain the subdomain data. Something like:

<VirtualHost *>
  ServerName www.domain.com
  DocumentRoot /home/httpd/htdocs/
</VirtualHost>

<VirtualHost *>
  ServerName subdomain.domain.com
  DocumentRoot /home/httpd/htdocs/subdomain/
</VirtualHost> 

and there also can be redirect entries in .htaccess files like

rewritecond %{http_host} ^subdomain.domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/subdomain/ [r=301,nc]

however in this case the user gets directed to the directory which contains the subdomain data but the user gets "out" of the subdomain.

I would like to know - how, when going to subdomain.domain.com the subdomain.domain.com, beginning of address remains visible in the address bar of the explorer?
Can it be done by an alternate entry in .htaccess file?
If a VirtualHost entry is specified in the vhosts.conf file, does it mean, that a new user account has to be specified for access to this directory?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

HTTP/1.1 requires that every host contain the name of the server being requested. So a request to http://www.foo.bar.com will first do a DNS lookup to see what server is associated with www.foo.bar.com. This might be a CNAME entry pointing to a web server, and maybe this same web server serves the content for http://courses.cs.bar.com. Both addresses go to the same place, but Apache can distinguish between the two because it sees where the browser is trying to go with every request.

So assuming DNS sends www.domain.com and subdomain.domain.com traffic to the same server, that server has a different configuration for each and is able to serve different documents.

Using two virtual hosts on a single host is functionally equivalent to having two separate physical web servers.

A new user account does not need to be specified for access. All files are accessed by the user running apache, which is usually the apache user. This user must be able to read all files it serves, in both document roots.

The rewrite rule takes effect before the virtualhost processing. In this situation, The web server rewrites the URL to change the client to point to a different url. mod_rewrite also sends 301 to the browser, telling it that the content lives somewhere else. According the HTTP specification, any browser receiving this code has to switch to requesting from the given HTTP address (replacing the old one in the browser address line).

This is like every time the user goes to the subdomain, giving them a page that says "this is somewhere else" and asking them to type in a new address into their browser.

Since VirtualHost entries are part of the main server configuration, not directory-specific options, they cannot be put into a .htaccess file.

To respond to your question about a config for two subdomains:

<VirtualHost *>
    ServerName www.domain.com
    DocumentRoot /public_html
</VirtualHost>

<VirtualHost *>
    ServerName subdomain.domain.com
    DocumentRoot /public_html/subdomain
</VirtualHost>

Be sure this is what you want; all of "subdomain"'s content will be accessible from the "www" address if the user appends "subdomain" to the URL.

For your second question, this can be done with a ServerAlias. This basically means that there's one virtual server, but it can be accessed by multiple names.

<VirtualHost *>
    ServerName www.domain.com
    ServerAlias subdomain.domain.com
    DocumentRoot /public_html
</VirtualHost>

And as for the last, if you have access to modify both DNS and your main Apache configuration you don't need anything else. I don't know how your specific hosting provider works; you may have to work it out with them if you don't have access to those two things.

share|improve this answer
    
Thank you for explaining this. So I guess .htaccess and rewriterule is not what I would want. But is the virtual host what is needed here? What would need to be done to point www.domain.com to /public_html and subdomain.domain.com to /public_html/subdomain? and (another version) could it be made so that both www.domain.com and subdomain.domain.com both point to /public_html/ ? As for the user account - I am talkig about a hosting providers' server and was just wondering if they would need to make another account for me... –  Priednis Jan 6 '11 at 13:56
    
@Priednis I've updated my responses to answer your new questions –  Michael Lowman Jan 6 '11 at 14:18
    
Great! Thank you for your answer! –  Priednis Jan 6 '11 at 16:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.