views:

73

answers:

2

So a lot of web apps have the customer choose their own subdomain, ie mycompany.webapp.com

Presumably, every subdomain works off the same set of files and the unique subdomain is perhaps stored as a session object. So how does the URL rewriting work?

If someone goes to mycompany.webapp.com, you have to rewrite their unique address to a dynamic page to set the session variable, ie webapp.com/mypage.php?cusomer=mycompany

But then when you redirected, you'd be at webapp.com/theappdirectory/ and not mycompany.webapp.com/

So how do they do it?

PS running IIS but the method should be the same for any server. The big thing for us is we cant do webapp.com/theappdirectory/mycompany/somefile.html, ie all the files reside in one directory and we'll have to set a session variable somehow whilst keeping the subdomain masked.

A: 

The redirect can just happen internally:

RewriteCond %{HTTP_HOST} ^([^./]+)\.example\.com$
RewriteCond %1 !=www
RewriteRule ^ mypage.php?customer=%1

To have this work your server/virtual host needs to be configured to accept all sub domains (see for example Name-based Virtual Host Support).

Gumbo
Would this mask the subdomain? So you'd browse as if it still were mycompany.webapp.com even though you were on say webapp.com/theappdirectory/somepage.htmlOn others I've seen, your web browser would show mycompany.webapp.com/somepage.html
Igor K
@Igor K: Since the redirect is only internal, the actual location is not shown. But this would still allow to request the actual file directly.
Gumbo
A: 

Any web address (inclusive of subdomains) map to an ip address using DNS.

  1. Now these addresses can map to separate web servers where you have documentroot set to whatever you want.

  2. But your question is makes sense when multiple domains/sub-domains are hosted on same physical machine. If you are using apache there is this virtual host setting that can be used that logically divides different domain hosted on same server. It basically maps the Host-Address (like x.example.com, y.example.com) or IP address to a virtual host which sets the initial parameters such as DocumentRoot, PHPAdmin values ..

Check out http://httpd.apache.org/docs/2.0/vhosts/examples.html and http://httpd.apache.org/docs/1.3/vhosts/ for more info

Srikanth
Thanks but I know I'd have a wildcard for all subdomains to point to the same server, I'm wanting to know how they can all share the same set of files for the web app whilst still keeping the subdomain masked.
Igor K