forked from zendframework/zf-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rewrites.php
88 lines (78 loc) · 2.76 KB
/
rewrites.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
namespace ZfSite;
if (!isset($_SERVER['REQUEST_URI'])) {
return;
}
$rewriteTable = array(
'/zf2/blog' => '/blog/index.html',
'/cla' => '/framework_cla_1.0.pdf',
'/ccla' => '/framework_cla_corporate_1.0.pdf',
'/code' => '/code/',
'/download/gdata' => '/downloads/latest',
'/download/gdata/' => '/downloads/latest',
'/downloads/gdata' => '/downloads/latest',
'/downloads/gdata/' => '/downloads/latest',
'/zf2' => '/',
'/zf2/status' => '/',
'/zf2/board' => '/',
'/zf2/board/invite' => '/',
'/zf2/blog/feed' => '/blog/feed-rss.xml',
'/docs/quickstart' => '/manual/1.12/en/learning.quickstart.html',
);
$rewriteRegexes = array(
'#^/zf2/blog/entry/(?P<id>[^/]+)#' => '/blog/%id%.html',
'#^/manual/(?P<lang>[a-z]{2}(_[a-zA-Z]+)?)(?P<page>.*)$#' => '/manual/1.12/%lang%%page%',
'#^/manual(/(?P<version>\d+\.\d+)(/(?P<lang>[a-z]{2}(_[a-zA-Z]+)?)(/)?)?)?$#' => function ($uri, array $matches) {
$lang = isset($matches['lang']) ? $matches['lang'] : 'en';
$version = isset($matches['version']) ? $matches['version'] : '2.0';
if ('1.' === substr($matches['version'], 0, 2)) {
$file = 'manual.html';
} else {
$file = 'index.html';
}
return sprintf('/manual/%s/%s/%s', $version, $lang, $file);
},
'#^/community*#' => '/participate',
'#^/releases/(?P<path>[^?]+\.(?:zip|tar.gz|tgz))#' => 'http://packages.zendframework.com/releases/%path%',
);
$rewrite = function ($uri) {
header(sprintf('Location: %s', $uri), true, 301);
exit(0);
};
$test = function () use ($rewriteTable, $rewriteRegexes, $rewrite) {
$uri = $_SERVER['REQUEST_URI'];
$uri = parse_url($uri, PHP_URL_PATH);
$uri = rtrim($uri, '/');
if (!$uri) {
return;
}
if (isset($rewriteTable[strtolower($uri)])) {
$rewriteTo = $rewriteTable[strtolower($uri)];
if (is_callable($rewriteTo)) {
$rewriteTo = $rewriteTo();
}
$rewrite($rewriteTo);
return;
}
foreach ($rewriteRegexes as $regex => $rewriteUri) {
$matches = array();
if (!preg_match($regex, $uri, $matches)) {
continue;
}
if (is_callable($rewriteUri)) {
$rewriteUri = $rewriteUri($uri, $matches);
$rewrite($rewriteUri);
return;
}
foreach ($matches as $key => $value) {
if (is_int($key) || is_numeric($key)) {
continue;
}
$rewriteUri = str_replace(sprintf('%%%s%%', $key), $value, $rewriteUri);
}
$rewrite($rewriteUri);
return;
}
};
$test();
unset($rewriteTable, $rewriteRegexes, $rewrite, $test);