Dynamic Subdmains

Using a single web hosting account to host multiple sites

Dynamic Subdmains

Postby tbobker » Tue Jun 06, 2006 8:34 am

Hi, i know it is my first post, and yes i have searched this site to find what i want to know but there doesnt seem to be what im looking for.

What i want to do, is allow for registration on my website whereby they can choose a subdomain of my URL, on completion am email is sent to the recipient with an installation URL that when pressed will install a piece of software / a cms i have built on the subdomain they have just chosen.

Much like the free phpbb forum hosting that is on offer around, accept i want to use my cms rather than the forum.

If there is a post on this sort of thing in the forum, please reply with the URL or please help.

Thanks.
tbobker
 
Posts: 5
Joined: Tue Jun 06, 2006 8:26 am

Postby richardk » Tue Jun 06, 2006 9:00 am

Are you going to use one set of CMS files, or multiple sets?

If it's a single set of files, is the subdomain going to be a variable sent to the scripts? eg. "abc.domain.com" --> "/index.php?page=index&username=abc".

If it's multiple sets of files, are they going to be in sub directories of your main domain? eg. "abc.domain.com" --> "/abc/index.php". The installation script will have to setup the files, etc.

Is it your server? If it is you should setup dynamic virtualhosts with mod_vhost_alias for multiple files (to send them to the right sub directory).

To actually create the DNS records for the subdomains you have two options, you can allow all subdomains with wildcard DNS (even if there's not a user), or you can create a script to somehow create them (i don't know how, certainly not with mod_rewrite).

There's a lot of details needed before the code can be written.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby tbobker » Tue Jun 06, 2006 9:29 am

You have opened some interesting questions that i did not know. So i can use my cms and run each user through the same files adjusted depending on the user name - the username variable. Much like a dynamic page where the content has come from a database www.domain.com?page=front / www.domain.com?page=content etc...

I suppose this would be an easier option, but what would be the advantages and disadvantages of each?

If i used this way then mod-rewrite will mask the domain www.domain.com?username=me to www.me.domain.com

am i on the right track?
tbobker
 
Posts: 5
Joined: Tue Jun 06, 2006 8:26 am

Postby richardk » Tue Jun 06, 2006 10:18 am

I suppose this would be an easier option, but what would be the advantages and disadvantages of each?

Advantages (of one set of files):
  1. One set of files to maintain/update.
  2. Takes up less space (you'll never run out of file space with one set).
  3. The creation script would (probably) just be inserting into a database, no messing around with files.
Disadvantages:
  1. The files have to be exactly the same. This may not be a problem if you're using a templating system or the sites are supposed to all look the same.
  2. Mess up in one file, break up all the sites. (But you should be thoroughly testing all code before putting it live.)

If i used this way then mod-rewrite will mask the domain www.domain.com?username=me to www.me.domain.com

Or you could write a PHP function to grab the username from the HTTP_HOST variable, then you wouldn't even need mod_rewrite to add the username variable (you might want it for friendly URLs though).

Here's an example:
Code: Select all
function getUsername()
   {
      // set your domain (possibly from a config file/class)
      // no www. and escape the "."(s) with a "\", eg google.com becomes google\.com
      $yourdomain = 'domain\.com';
      if(($dom = getenv('HTTP_HOST')) === false)
         {
            // not HTTP_HOST variable, can't continue.
            // either
            die('Error...');
            // or set a default user
            return 'default';
         }
      if(!preg_match('#^(www\.)?([^\.]+)\.' . $yourdomain . '$#', $dom, $match))
         {
            // did not match, domain is possibly www.domain.com or domain.com
            // set default user.
            return 'default';
         }
      // return username
      return $match[2];
   }

// usage:
$username = getUsername();

You'd just need to point all the subdomains to the same place (with server configuration is best, depends how your host works), and it'd work.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby tbobker » Wed Jun 07, 2006 7:56 am

Hey thanks, interesting it does not seem that dificult. So is it not possible with one file set to store users options, like the design of there site in a separate file of there own, say they build there own stylesheet and some how is switched to theres depending on the username or subdomain that the browser is pointing at?

Sorry the code that you wrote was a little advanced for me, were you using regular expressions?

I am kind of guessing about what the code does, it finds the username and sticks it in front of the domain name, and the default is my normal domain?
tbobker
 
Posts: 5
Joined: Tue Jun 06, 2006 8:26 am

Postby richardk » Wed Jun 07, 2006 8:19 am

So is it not possible with one file set to store users options, like the design of there site in a separate file of there own, say they build there own stylesheet and some how is switched to theres depending on the username or subdomain that the browser is pointing at?

You can load whatever CSS/templates/config you want (in PHP) when you have the username variable. I was just saying that you'd have to design it to change depending on the username, you wouldn't just change a file. The best idea is to write a simple templating system.

Sorry the code that you wrote was a little advanced for me, were you using regular expressions?

There is one regex (the preg_match line). ^(www\.)?([^\.]+)\.' . $yourdomain . '$, with $yourdomain replaced becomes ^(www\.)?([^\.]+)\.domain\.com$. Regular Expressions Info.


I am kind of guessing about what the code does, it finds the username and sticks it in front of the domain name, and the default is my normal domain?

If you went to "richard.domain.com" or "www.richard.domain.com" and ran that function, it would return "richard". If it's www.domain.com or domain.com, it would return "default".
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Think i am with you

Postby tbobker » Thu Jun 08, 2006 1:44 pm

Yes i think iam getting it!
So when you go to www.me.domain.com the php script captures the word 'me' and because i have told it to use this as a username it can then ditermin which /perhaps which stylesheet to load up.

I suppose somewhere you would use the $_GET array, so you can capture the username?

Think im getting it.
tbobker
 
Posts: 5
Joined: Tue Jun 06, 2006 8:26 am

Postby richardk » Thu Jun 08, 2006 2:23 pm

I suppose somewhere you would use the $_GET array, so you can capture the username?

No. This function works like time() and other functions that run with no arguments and just return a value. So if i wanted to know the current time stamp, i'd use
Code: Select all
<?php

$current_time = time();

?>

and $current_time would hold the value.

So to use this function, all you'd do (as long as it was included) would be run the function:
Code: Select all
<?php

include('some_file_with_function_in_it.php');

$username = getUsername();

?>

and $username would hold the username. You don't need $_GET or anything.

it can then ditermin which /perhaps which stylesheet to load up.

Then, based on the $username variable, you could do anything, eg. a personalised stylesheet:
Code: Select all
<?php

include('some_file_with_function_in_it.php');

$username = getUsername();

echo '<style type="text/css"> @import url(/css/' . $username . '.css); </style>';

?>

So it'd be "/css/richard.css" from the subdomain "www.richard.domain.com".
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

thanks

Postby tbobker » Thu Jun 08, 2006 9:16 pm

Thanks, think i have the idea. Well it perhaps seems more simple than i thought. Will let you know of progress.
tbobker
 
Posts: 5
Joined: Tue Jun 06, 2006 8:26 am

Postby BADGROOVY » Tue Jul 11, 2006 8:17 pm

richardk wrote:
I suppose somewhere you would use the $_GET array, so you can capture the username?

No. This function works like time() and other functions that run with no arguments and just return a value. So if i wanted to know the current time stamp, i'd use
Code: Select all
<?php

$current_time = time();

?>

and $current_time would hold the value.

So to use this function, all you'd do (as long as it was included) would be run the function:
Code: Select all
<?php

include('some_file_with_function_in_it.php');

$username = getUsername();

?>

and $username would hold the username. You don't need $_GET or anything.

it can then ditermin which /perhaps which stylesheet to load up.

Then, based on the $username variable, you could do anything, eg. a personalised stylesheet:
Code: Select all
<?php

include('some_file_with_function_in_it.php');

$username = getUsername();

echo '<style type="text/css"> @import url(/css/' . $username . '.css); </style>';

?>

So it'd be "/css/richard.css" from the subdomain "www.richard.domain.com".



Hi Richardk, hanging from tbobker´s post ,instead of opening a new thread i have a doubt :


I want to use a similar php script on my index page, so when a user hits :
ANYSUBDOMAIN.mydomain.com , .htaccess forwards to mydomain.com, and i can catch ANYSUBDOMAIN, so i know what my users are typing for a subdomain ...

The subdomain's folders doesn´t exist, and i dont want to create them dynamically neither, i just want to know the name of the subdomain they are typing.

Searching on google for dynamic subdomains,i found out that i needed to add a CNAME for (*) and pointed it to my domain, (im hosting with godaddy.com) and they told me the same, after doing it so, instead of having a "page not found" message, now i get a "You don't have permission to access / on this server, Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request. " message.

I tried using an Error document 403, but it is not working, i tried having more support from godaddy, but they refused.

So, how can i tell apache via .htaccess that when it gets a request like this

ANYSUBDOMAIN.mydomain.com, it redirects to mydomain.com

I´ve seen some of your replies from similar questions, and i tried using this :

Code: Select all
RewriteEngine On

RewriteCond %{HTTP_HOST} !^(www\.)?DOMAIN$ [NC]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^([^\.]+)\.DOMAIN$ [NC]
RewriteRule .* /%1%{REQUEST_URI} [QSA,L]


But i still get the "forbidden,permission error".


Hope u have a clue of what to do here.

Thks in advance. :D
BADGROOVY
 
Posts: 6
Joined: Tue Jul 11, 2006 7:47 pm

Next

Return to Domain Handling

Who is online

Users browsing this forum: No registered users and 30 guests

cron