Home » Questions » Computers [ Ask a new question ]

How to make subdomain user accounts in a webapp

How to make subdomain user accounts in a webapp

"I am looking to allow users to control of subdomain of an app I am toying with, much like Basecamp where it is customusername.seework.com.

What is required on the DNS end to allow these to be created dynamically and be available instantly.

And how do you recommend dealing with this in the logic of the site? Htaccess rule to lookup the subdomain in the DB?"

Asked by: Guest | Views: 166
Total answers/comments: 4
Guest [Entry]

"The way we do this is to have a 'catch all' for our domain name registered in DNS so that anything.ourdomain.com will point to our server.

With Apache you can set up a similar catch-all for your vhosts. The ServerName must be a single static name but the ServerAlias directive can contain a pattern.

Servername www.ourdomain.com
ServerAlias *.ourdomain.com

Now all of the domains will trigger the vhost for our project. The final part is to decode the domain name actually used so that you can work out the username in your code, something like (PHP):

list( $username ) = explode( ""."", $_SERVER[ ""HTTP_HOST"" ] );

or a RewriteRule as already suggested that silently maps user.ourdomain.com/foo/bar to www.ourdomain.com/foo/bar?user=user or whatever you prefer."
Guest [Entry]

"Don't worry about DNS and URL rewriting

Your DNS record will be static, something like:


Ask your DNS provider to do it for you (if it's not done already) or do it by yourself if you have control over your DNS records. This will automatically point all your subdomains (current and future ones) into the same HTTP server.

Once it's done, you will only need to parse HOST header on every single http request to detect what hostname was used to access your server-side scripts on your http server.

Assuming you're using ASP.NET, this is kind of silly example I came up with but works and demonstrates simplicity of this approach:

<%@ Language=""C#"" %>
string subDomain = Request.Url.Host.Split('.')[0].ToUpper();
if (subDomain == ""CLIENTXXX"") Response.Write(""Hello CLIENTXXX, your secret number is 33"");
else if (subDomain == ""CLIENTYYY"") Response.Write(""Hello CLIENTYYY, your secret number is 44"");
else Response.Write(subDomain+"" doesn't exist"");
Guest [Entry]

The trick to that is to use URL rewriting so that name.domain.com transparently maps to something like domain.com/users/name on your server. Once you start down that path, it's fairly trivial to implement.
Guest [Entry]

"I was looking to do something similar (www.mysite.com/SomeUser).

What I did was I edited 404.shtml to include this server side include (SSI) code:

<!--#include virtual=""404.php"" -- >

Then I created the file 404.php, where I parsed the URL to check for a user's name and showed their info from the database."