Skip to content

Instantly share code, notes, and snippets.

@picasso250
Last active August 29, 2015 13:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save picasso250/9460238 to your computer and use it in GitHub Desktop.
Save picasso250/9460238 to your computer and use it in GitHub Desktop.
php -S 0.0.0.0:80 router.php
<?php
// php build in server vhost
// php -S 0.0.0.0:80 router.php
// on Windows, router.php must be absolute path, bug of php v5.4.26
$map = array(
'host.name' => '/file/path',
);
$f = __DIR__.'/router_config.php';
if (file_exists($f)) {
$map = require $f;
}
$host = $_SERVER['HTTP_HOST'];
if (isset($map[$host])) {
error_log("$host $_SERVER[REQUEST_URI]");
$root = $map[$host];
$arr = explode('?', $_SERVER["REQUEST_URI"]);
$path = $arr[0];
$f = $root.$path;
if ($f == "$root/favicon.ico") {
if (is_file($f)) {
header("Content-Type: image/x-icon");
readfile($f);
} else {
return false;
}
}
$fhtml = "$root/index.html";
$fphp = "$root/index.php";
if (is_file($f)) {
$pathinfo = pathinfo($f);
if (isset($pathinfo['extension']) && $pathinfo['extension'] == 'php') {
chdir(dirname($f));
include $f;
} else {
$meme = get_content_type($f);
header("Content-Type: $meme");
readfile($f);
}
} elseif (is_file($fhtml)) {
echo file_get_contents($f);
} elseif (is_file($fphp)) {
chdir($root);
include $fphp;
}
} else {
return false;
}
function get_content_type($f)
{
static $map = array(
'js' => 'application/javascript',
'css' => 'text/css',
'png' => 'image/png',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'gif' => 'image/gif',
'ico' => 'image/x-icon',
);
$pathinfo = pathinfo($f);
if (isset($pathinfo['extension']) && ($extension = strtolower($pathinfo['extension'])) && isset($map[$extension])) {
return $map[$extension];
}
return 'text/html';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment