I use that which you are describing, this is my code.
<?
if ($HTTP_HOST == 'domain.com') {
Header ("Location: http://www.domain.com$PHP_SELF");
die;
}
elseif ($HTTP_HOST == 'www.domain.com') {
?>
Your html, text...whatever.
<?
}
?>
Nothing fancy, but it works.