Skip to content

Instantly share code, notes, and snippets.

@enix-app
Last active September 20, 2020 10:34
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 enix-app/7b07a015bc6a4541bf052177d3c27c6d to your computer and use it in GitHub Desktop.
Save enix-app/7b07a015bc6a4541bf052177d3c27c6d to your computer and use it in GitHub Desktop.
An alternative router.php
<?php
define('__SELF__', getcwd());
if (preg_match('/\.(?:png|jpg|jpeg|gif|js|css|html)$/', $_SERVER["REQUEST_URI"])) {
$ext = pathinfo($_SERVER["REQUEST_URI"])['extension'];
$extensions = [
'js' => 'text/javascript',
'css' => 'text/css',
'html' => 'text/html',
'jpg' => 'image/jpg',
'png' => 'image/png',
'jpeg' => 'image/jpeg',
'gif' => 'image/gif',
];
if(isset($extensions[$ext])) {
header("Content-Type: {$extensions[$ext]}");
}
include __SELF__ . '/../' . $_SERVER['REQUEST_URI'];
return true;
}
if (is_dir(__SELF__ . $_SERVER['PHP_SELF'])) {
include __SELF__ . $_SERVER['PHP_SELF'] . '/index.html';
}
else {
$dir = rtrim(__SELF__ . '/../', "/") . $_SERVER['REQUEST_URI'];
if(is_file($dir . 'index.html')) {
include $dir . 'index.html';
}
else if(is_file($dir . 'index.php')) {
include $dir . 'index.php';
}
else {
echo "<h2>Not Found!</h2>";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment