apacheThere are several cases when you would like to use dynamic subdomains. In case your webhosting don’t support unlimited subdomains or when you’re developing a community website with a focus on the user profiles. In this case I recommend you to write a script which allows users to create personal pages with a clear address like ‘crispijn.yourdomain.com’.

Obviously do I know the .htaccess solution to create subdomains. This is very handy if you don’t have cPanel or another web admin tool.

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\.yoursite\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.yoursite\.com [NC]
RewriteRule ^(.*)$ http://www.yoursite.com/%1 [L,R]

This solution points to a folder in your root so it isn’t a very dynamic one. Lets see the solution!

Apache configuration

First you have to change the Apache configuration to make it by adding the following line to the httppd.conf file (the Apache configuration file).

ServerAlias *.yourdomain.com

Read more about this configuration on the Apache website.

Static subdomains

//array for static subdomains
$subdomains = array(
			'forum',
			'mobile',
			'demos',
			);

$match = preg_replace('#(http://)?(www\.)?(.+?)(\.yourdomain.com){1}#i','\\3', $_SERVER['HTTP_HOST']);

	if(in_array($match, $subdomains)){
		include('/root/yourdomain.com/'.$matc.'/index.php');
	}else{
		//Do normal stuff
	}

Dynamic sub domains

It is also possible to create the array from a database selection. See the following example:

//array for static subdomains
$subdomains = array(
			'forum',
			'mobile',
			'demos',
			);

$match = preg_replace('#(http://)?(www\.)?(.+?)(\.yourdomain.com){1}#i','\\3', $_SERVER['HTTP_HOST']);

	if(in_array($match, $subdomains)){
		include('/root/yourdomain.com/'.$matc.'/index.php');
	}else{
		$sql = "SELECT nickname FROM users WHERE nickname = ".$match;
		$res = mysql_query($sql);
		$rows = mysql_num_rows($res);

		if($rows == 1){
			//pointer to user profile page
			include('/root/yourdomain.com/profiles/index.php);
		}else{
			//Do normall stuff
		}
	}

NOTE!
This is a very basic way of creating subdomains. There are no build in checks or errorhandler. The way of naming your files and choosing a structure is whole yours.

Sessions

When you’re using sessions (a login system for example) the session will be lost you’re browsing to another (sub)domain. You can prevent this by adding the following line to your .htaccess file in the root of your site.

session.cookie_domain = "yourdomain.com"

Related Posts

Comments (4)

avatar

Chris says:

I like the dynamic way of adding countless subdomains to my website! Thanks for all!

Sohan Soni says:

Sir i want to create subdoamin structure for my website mlmsoftware4u.com. I have already made above mentioned methods directed by you. Please suggest is it necessary to have dedicated server for the same or not?

avatar
avatar

Crispijn says:

No, I don’t think you need a dedicated server to do this. Just configure Apache like the way I described and that should be all.

dany says:

if i have web and have category like http://localhost/economy,can be possible use that class ?
thank you..

avatar

Leave a Comment

Get your own Gravatar!
Your email will never be published!

Notify me of followup comments via e-mail

Top of Page