Skip to content

Instantly share code, notes, and snippets.

@aquarion
Created August 27, 2010 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aquarion/553417 to your computer and use it in GitHub Desktop.
Save aquarion/553417 to your computer and use it in GitHub Desktop.
<?PHP
// @todo: deal with multiple options a bit better.
header("HTTP/1.1 404 File Not Found");
$purl = parse_url($_SERVER['REQUEST_URI']);
$host = $_SERVER['HTTP_HOST'];
$error = "File not Found";
if (!file_exists("/var/www/hosts/".$host)){
$error = "Host not configured";
if(file_exists("/var/www/hosts/www.".$host)){
header("location: http://www.".$host.$_SERVER['REQUEST_URI']);
die("No host, but there is a www version of it");
}
if(preg_match("#^www\.#", $host) && file_exists("/var/www/hosts/".substr($host, 4))){
header("location: http://".substr($host,4).$_SERVER['REQUEST_URI']);
die("No host, but there is a non-www version of it");
}
$bom = explode(".", $host);
$check = implode(".",array_slice($bom, 0,-1));
$glob = glob("/var/www/hosts/".$check.".*");
if(count($glob) > 1){
// Many options, do something clever.
} elseif(count($glob)){
header("location: http://".basename($glob[0]));
die("Found you");
}
$check = implode(".",array_slice($bom, 0, 1));
$glob = glob("/var/www/hosts/www.".$check.".*");
if(count($glob) > 1){
$try = $glob;
} elseif(count($glob)){
header("location: http://".basename($glob[0]));
die("Found you");
}
$check = implode(".",array_slice($bom, 0, 1));
$glob = glob("/var/www/hosts/*.".$check.".*");
if(count($glob) > 1){
// Many options, do something clever.
} elseif(count($glob)){
header("location: http://".basename($glob[0]));
die("Found you");
}
}
?>
<html>
<head>
<title>Not Found</title>
<style type="text/css">
body {
margin: 0;
font-family: sans-serif;
}
.header {
background-color: #1b6de3;
color: #FFF;
border-bottom: 2px solid #0e3874;
height: 100px;
padding: 1em;
}
.content {
padding: 1em;
}
</style>
</head>
<body>
<div class="header">
<a href="http://www.aqxs.net"><img src="http://www.aqxs.net/aqxs.png" border="0"></a>
<br/>Hosting
</div>
<div class="content">
<h1><?=$error?></h1>
<p>Sorry, I haven't been able to complete that request for you.</p>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment