-
-
Notifications
You must be signed in to change notification settings - Fork 705
/
router.php
53 lines (48 loc) · 1.58 KB
/
router.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
/* router.php
*
* Use `php -S localhost:8000 router.php` to include simple URL rewriting for
* your local PHP development server. NOT intended for production whatsoever.
*/
if (getenv('PHPENV') === 'production') {
header('HTTP/1.1 403 Forbidden');
echo 'Script only accessible in development environment.';
exit;
}
// Allow query parameters to be appended to the request
$requestUri = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
if (preg_match('#^/([a-z]{2}(?:_(?:[A-Z]{2}|[A-Z][a-z]+))?)(/.*)?$#', $requestUri, $matches)) {
$_GET['lang'] = $matches[1];
$requestUri = (!empty($matches[2])) ? $matches[2] : '/';
}
if ($requestUri == '/') {
$page['name'] = 'index';
include 'index.php';
} elseif (preg_match('/\./', $requestUri)) { // Has period in filename
$target = '.'.$requestUri;
if (!file_exists($target)) {
header('HTTP/1.1 404 Not Found');
include '404.php';
} else {
return false;
}
} elseif (strpos($requestUri, '/docs/') === 0 || $requestUri == '/docs') {
// For documentation (MDR)
include __DIR__.'/docs/_mdr/index.php';
} elseif (substr($requestUri, -1) === '/') { // Ends with a slash
$target = '.'.$requestUri.'index.php';
if (file_exists($target)) {
include $target;
} else {
header('HTTP/1.1 404 Not Found');
include '404.php';
}
} else {
$target = '.'.$requestUri.'.php'; // Rewrite extension-less files as php files
if (file_exists($target)) {
include $target;
} else {
header('HTTP/1.1 404 Not Found');
include '404.php';
}
}