Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make subdomain user accounts in a webapp

Tags:

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?

like image 320
Caleb Elston Avatar asked Aug 04 '08 12:08

Caleb Elston


People also ask

How do I create a subdomain mailbox?

To setup, simply navigate to Web Host Manager -> Create A New Account. Rather than using a domain, enter the subdomain (i.e. billing.yourdomain.com) and use the password generator to create a strong one. Once added, login to cPanel -> Email Accounts and create the address needed.

What is an account subdomain?

A subdomain name is a piece of additional information added to the beginning of a website's domain name. It allows websites to separate and organize content for a specific function — such as a blog or an online store — from the rest of your website.

Can I use a subdomain for a different website?

You should only use subdomains if you have a good reason to do so. For example, you can use subdomains to rank for different keywords, target a specific market, or reach a different location or serve a language other than that of your main website. Subdirectories are files found under your primary domain.


2 Answers

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.

like image 68
Mat Avatar answered Oct 10 '22 14:10

Mat


Don't worry about DNS and URL rewriting

Your DNS record will be static, something like:

*.YOURDOMAIN.COM A 123.123.123.123 

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"); %> 
like image 29
lubos hasko Avatar answered Oct 10 '22 13:10

lubos hasko