<?php
$url = strtolower($_SERVER["HTTP_HOST"]);
if(strpos($url, "//") !== false) { // remove http://
$url = substr($url, strpos($url, "//") + 2);
}
$urlPagePath = "";
if(strpos($url, "/") !== false) { // store post-domain page path to append later
$urlPagePath = substr($url, strpos($url, "/"));
$url = substr($url, 0, strpos($url,"/"));
}
$urlLast = substr($url, strrpos($url, "."));
$url = substr($url, 0, strrpos($url, "."));
if(strpos($url, ".") !== false) { // get rid of subdomain(s)
$url = substr($url, strrpos($url, ".") + 1);
}
$url = "http://www." . $url . $urlLast . $urlPagePath;
header( "Location:{$url}");
?>