So, you want to have something like http://corporate.purpleoranges.com/ automatically work with your CMS or whatever you use and have it redirect to http://www.purpleoranges.com/site/corproate.html or something like that?
Here is a snippet of php code that will do just that. Before this will work – you will need to have a wildcard CNAME setup to point to your web site, and you (if you are using host-header) will need to let your web server know that it should process requests for all these sub domains.
# Redirect Based on domain name
# 061229 Troy Kelly
# This code is covered by a creative commons licence.
# You are free:
# * to Share — to copy, distribute, display, and perform the work
# * to Modify — to make derivative works
# Please see the requirements placed on you here http://creativecommons.org/licenses/by-sa/2.5/au/
$basedomain = ‘my.com’; # The base url for your site DO NOT INCLUDE WWW
# eg if your site is http://www.purpleoranges.com/
# put purpleoranges.com
$appurl = ‘/user.php?username=%sub%’; # Where you want to redirect to at the base URL (above)
# So it will become http://my.com/user.php?username={subdomain}
# Should not need to change the rest…
$basesections = count(explode( ‘.’ , $basedomain ));
$domainparts = explode( ‘.’ , $_SERVER["HTTP_HOST"]); #Get all the parts of the domain into an array
if (count($domainparts) > $basesections) {
if ($domainparts[(count($domainparts) - ($basesections+1))] <> ‘www’) {
$redirect = ‘http://www.’.$basedomain.str_replace(“%sub%”, $domainparts[(count($domainparts) - ($basesections + 1))], $appurl);
header ( “HTTP/1.1 301 Moved Permanently” );
header(“Location: $redirect”);
}
}
