views:

69

answers:

6

In my project i have to make a subdomain, i.e

if the user name is XXX when he register, a sub domain will be created like XXX.example.com

how to do it?

I will use php for scripting.

regards

tarique

+1  A: 

This might be a little more complex than you think.

I suggest to do some reading on mod rewriting and htaccess.

You could start here:

htaccess Tutorial

Modrewrite tutorial

Subdomain Modrewrite Example

EDIT: Or just go with one of the nice examples provided my fellow SO users. ;)

KB22
Thanks, I think i have to study it from scratch.
TIT
+1  A: 

I found a script that seems to do exactly that, create a subdomain on your server on demand. It probably needs a little bit of tweaking for it to work on your particular control panel, but the review are quite positive as far as I can tell.

Link

Have you considered using htaccess and url rewriting? Found this code that may help you:

# Rewrite <subdomain>.example.com/<path> to example.com/<subdomain>/<path>
#
# Skip rewrite if no hostname or if subdomain is www
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
# Extract (required) subdomain (%1), and first path element (%3), discard port number if present (%2)
RewriteCond %{HTTP_HOST}<>%{REQUEST_URI} ^([^.]+)\.example\.com(:80)?<>/([^/]*) [NC]
# Rewrite only when subdomain not equal to first path element (prevents mod_rewrite recursion)
RewriteCond %1<>%3 !^(.*)<>\1$ [NC]
# Rewrite to /subdomain/path
RewriteRule ^(.*) /%1/$1 [L]

Source (Post #6)

johnnyArt
+1  A: 

As long as this is for non-SSL sites, then by far the easiest way is not to bother - just use a wildcard DNS domain and vhost, then map any domain specific behaviours in your PHP code. If you need SSL sites then its a lot more complicated - you need to have a seperate IP address/port for each certificate - and woldcard certs can be very expensive.

If you're wanting to set up some sort of hosting package then its a bit more involved - how you go about this depends on what webserver and DNS server you are using.

Assuming (again no SSL) with Apache on Unix/POSIX/Linux and bind, then, again I'd go with a wildcard DNS entry, then: 1) create a base dir for the website, optionally populate this with a default set of files 2) add a vhost definition in its own file in /etc/httpd/conf.d named as XXX.conf 3) send a kill -HUP to the HTTPD process (causes it to read the new config files without having to do a full restart).

One thing to note is that you really shouldn't allow the httpd process direct write access to its own config files - you definitely don't want to give it root privileges. A safer solution would be to create a CLI script to perform this using the username as an argument then make it setuid and invoke it from the script run by the HTTPD process.

C.

symcbean
A: 

the best way is to use a joker in your DNS server :

www.example.com.    IN  A 1.2.3.4
*.example.com.  IN  A 1.2.3.4

By this way, No subdomain has to be created : all are pointing to the same IP by default.

In your PHP code, you just have get $_SERVER["HOST"] and get the fist part :

$hostParts=explode('.',$_SERVER["HTTP_HOST"]);
$user=$hostParts[0]
Eric
A: 

First, you need to make sure you have a wildcard domain setup in DNS, and make sure your webserver (apache?) directs all queries for that wildcard domain to your php file.

Then in php you can look at $_SERVER["HTTP_HOST"] to see which subdomain is used for that particular request.

warpr
A: 

Since you will make sub-domains when an user registers. Try this as .htaccess file:

Options +FollowSymlinks 
RewriteEngine on 
RewriteRule ^.htaccess$ - [f]

RewriteCond %{HTTP_HOST}!^www.domain.com 
RewriteCond %{HTTP_HOST} ^([^.]+).domain.com 
RewriteRule ^$(.*) /$1/%1 [L]

make a function of a controller which will take the value of sub-domain and display what necessary. like::

public function show ($domain)
{
  **.**..*..**.** Your code goes here
}

when a user will try this xxx.domain.com/controller/show this will be domain.com/controller/show/xxx . if you want to xxx.domain.com to be domain.com/controller/show/xxx just edit the htaccess file as you want.

Mazhar Ahmed