-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.php
More file actions
57 lines (51 loc) · 1.8 KB
/
router.php
File metadata and controls
57 lines (51 loc) · 1.8 KB
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
54
55
56
57
<?php
/**
* Dev server router - mimics .htaccess rewrites
* Usage: php -S localhost:8888 router.php
*/
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
// Serve static files directly
if ($uri !== '/' && file_exists(__DIR__ . $uri) && !is_dir(__DIR__ . $uri)) {
return false;
}
// Route rewrites (mirrors .htaccess)
$routes = [
'#^/cam/(\d+)$#' => '/cam.php?id=$1',
'#^/country/([^/]+)$#' => '/search.php?country=$1',
'#^/city/([^/]+)$#' => '/search.php?city=$1',
'#^/place/([^/]+)$#' => '/search.php?tag=$1',
'#^/manufacturer/([^/]+)$#' => '/search.php?manufacturer=$1',
'#^/search$#' => '/search.php',
'#^/map$#' => '/map.php',
'#^/about$#' => '/about.php',
'#^/contact$#' => '/contact.php',
'#^/privacy$#' => '/privacy.php',
'#^/sitemap\.xml$#' => '/sitemap-generator.php',
'#^/view/(\d+)$#' => '/cam.php?id=$1',
];
foreach ($routes as $pattern => $target) {
if (preg_match($pattern, $uri, $matches)) {
$file = preg_replace($pattern, $target, $uri);
// Parse target to separate file and query string
$parts = explode('?', $file, 2);
$scriptFile = $parts[0];
if (isset($parts[1])) {
parse_str($parts[1], $params);
$_GET = array_merge($_GET, $params);
$_SERVER['QUERY_STRING'] = http_build_query($_GET);
}
$_SERVER['SCRIPT_NAME'] = $scriptFile;
$_SERVER['SCRIPT_FILENAME'] = __DIR__ . $scriptFile;
require __DIR__ . $scriptFile;
return true;
}
}
// Default: serve index.php for root
if ($uri === '/') {
require __DIR__ . '/index.php';
return true;
}
// 404
http_response_code(404);
echo '<h1>404 Not Found</h1>';
return true;