How to Get the Root Domain Name from the URL of a Page (HOST)

There are times when you have multiple domains pointing to the same host, and you require a script to detect which domain the visitor is using.

This function returns the root domain via PHP, which you can use to redirect the visitor to another page or folder, to customize which ads are displayed, determine which database to connect to, etc.

The script makes use of the $_SERVER["HTTP_HOST"] variable, as well as the regular expression function ereg_replace, and the PHP function parse_url.

Here is the GetRootDomain() function:

Here is an example of how to call the function, assuming the function is in a file called


© 2011 by