forked from cakephp/bakery.cakephp.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redirects.php
44 lines (34 loc) · 982 Bytes
/
redirects.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
<?php
$url = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : $_SERVER['REQUEST_URI'];
$root = __DIR__ . '/';
$byebye = function () {
header('HTTP/1.1 301 Moved Permanently');
header('Location:/');
exit;
};
if (!preg_match('#/articles/[A-Za-z0-9_\-]+/([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)#', $url, $matches)) {
$byebye();
}
$year = $matches[1];
$month = $matches[2];
$day = $matches[3];
$article = $matches[4];
$path = implode('/', [$year, $month, $day]) . '/';
if (!is_dir($root . $path)) {
$byebye();
}
$files = array_map(function ($file) {
return current(explode('.rst', $file));
}, array_map('basename', glob($path . '*.rst')));
$files = array_map(function ($file) {
return [strtolower(str_replace('-', '_', $file)), $file];
}, $files);
foreach ($files as $file) {
similar_text($file[0], $article, $percent);
if ($percent >= 90) {
header("HTTP/1.1 301 Moved Permanently");
header('Location:/' . $path . $file[1] . '.html');
exit;
}
}
$byebye();