Subscribe to Tutorial Feed

Flash and PHP Bible

The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area.

The Flash and PHP Bible has a forum for quick support.

Scriptplayground » tutorials » php » Grab Subdomain From Url

Grab Subdomain From Url

Use this block of code to grab the subdomain from the URL. You can then use it in any part of your code.

This requires your host to have: #ServerAlias *.domain.com in the .htaccess file of in the config of Apache otherwise Apache just redirects to a domain name.

<?php

// Whether or not to output debug code
//DEFINE("DEBUG", 1);
DEFINE("DEBUG", 0);

// An array of actual subdomains on the server
$real_subdomains = array('subdomain1','subdomain2');

// The domain info for this site
$domain_name = 'domain';
$domain_tld = '.com';

// Set a variable to hold the host info
$host_info = '';

// Check to see if 'register_globals' is on
if(ini_set('register_globals' == 1))
	{
	$host_info = $HTTP_HOST;
	}
else
	{
	$host_info = $_SERVER['HTTP_HOST'];
	}

// Break up the pieces of the host info
$parts = explode(".", $host_info);

// Pull the 'subdomain' for the url
$subdomain = $parts[0];

(DEBUG) ? print "Host Info: " . $host_info : '';

// Check that the subdomain isn't 'www' or the domain name
if(($subdomain != "www") && ($subdomain != $domain_name))
	{
	// Is the subdomain a real domain?
	if(in_array($subdomain, $real_subdomains))
	{
	// Redirect and exit
	header("Location: http://www." . $domain_name . $domain_tld . "/" . $subdomain . "/");
	exit();
	}
	else
	{
	header("Location: http://www." . $domain_name . $domain_tld . "/");
	}
	}

?>

The above code has been commented for easy use. This will work if "register_globals" is on or off.

Follow Scriptplayground on Twitter (@scriptplay)

Comments: Grab Subdomain From Url

 Jhecht  Sun Jun 10, 2007 5:54 pm  
I did this function a bit backwards. I would have a subdomain, but i wanted to get the base domain from the subdomain. I came up with this simple function :

function find_base_domain($url=false){
if($url== false){
$url = (@is_array($_SERVER)) ? $_SERVER['SERVER_NAME'] : ((@is_array($HTTP_SERVER_VARS)) ? $HTTP_SERVER_VARS['SERVER_NAME'] : $SERVER_NAME );
//If $_SERVER is an array, give the server name, if $HTTP_SERVER_VARS is an array, give the server name from that, if not, then just use the old old $SERVER_NAME variable;
}
//Continue with the rest of the function;
$url = str_replace("http://","",$url);
$domainExt = substr($url,strrpos($url,"."));

$domain = substr($url,0,strrpos($url,"."));

if(strstr($domain,".")){
$base_domain = substr($domain,strrpos($domain,".")+1);
}else{
$base_domain = $domain;
}
return $base_domain.$domainExt;


}

I plan on making it so it takes another argument, being $level, which says how many levels back you want to go(since some sites have subdomain1.subdomain2.domain.com and so on(like yahoo) ).
 mkeefe  Mon Jun 11, 2007 3:01 am  
That is a very useful function, do you mind if I post it in the forum resources with full credit given?
 Chris Williams  Thu Aug 6, 2009 8:20 pm  
Thanks for the post :-)

I found this to work as well... though, the 2 in the second line might need to be changed to 3 if the TLD is co.uk or the like...


$urlParts = explode(".",$_SERVER["HTTP_HOST"]);
$subDomain = implode(".",array_slice($urlParts,0,count($urlParts) - 2));
 Brian  Thu Jan 13, 2011 11:01 am  
Or you could just use this:

$subdomain = implode(array_slice(array_reverse(explode('.', $_SERVER['SERVER_NAME'])),2));
 Brian  Thu Jan 13, 2011 11:05 am  
In my above example, the reason I reversed the array was so that URLs WITHOUT subdomains wouldn't falsely return the domain itself as a subdomain.

The caveat of changing the 2 to a 3 also applies in TLD's like ".co.uk" etc...
 Brian  Thu Jan 13, 2011 11:10 am  
Updated to accomodate chained subdomains:

$subdomain = implode(".",array_reverse(array_slice(array_reverse(explode('.', "sub2.sub1.domain.tld")),2)));

RETURNS: sub2.sub1

Remove the outer ARRAY_REVERSE or IMPLODE according to your needs.
 Jan Miller  Fri Jun 24, 2011 3:31 pm  
Could you please elaborate on your disclaimer: "This requires your host to have: #ServerAlias *.domain.com in the .htaccess file of in the config of Apache otherwise Apache just redirects to a domain name. "
Is this something we need to verify with our hosting company or something that we can check and modify on our own?
Thanks
 mkeefe  Mon Jun 27, 2011 10:39 am  
@Jan - You'll want to check with your host, unless you have a dedicated server (where you can change configuration settings).
Add a comment
The ability to add new comments has been disabled in this archived version.

Loading
Main | Tutorials | Articles