/
index.php
38 lines (30 loc) · 1009 Bytes
/
index.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
<?php
if (file_exists('/tmp/301_cache.json')) {
$cache = json_decode(file_get_contents('/tmp/301_cache.json'));
} else {
$cache = new StdClass;
}
$host = $_SERVER['SERVER_NAME'];
if (!property_exists($cache, $host)) {
$txt_records = dns_get_record($host, DNS_TXT);
foreach ($txt_records as $txt_record) {
if (!preg_match('#^(30[123]) (.*)$#', $txt_record['txt'], $matches)) {
continue;
}
if (!filter_var($matches[2], FILTER_VALIDATE_URL)) {
die("Invalid URL {$matches[2]}");
}
$cache->{$host} = $matches[2];
}
if (!property_exists($cache, $host)) {
readfile('mainpage.html');
exit;
die ("Please add TXT record '301 http://your-target-url' in domain name '{$host}'");
}
}
$url = $cache->{$host};
if (preg_match('#\*$#', $url)) {
$url = rtrim(rtrim($url, '*'), '/') . $_SERVER['REQUEST_URI'];
}
header('X-Target: ' . $cache->{$host});
header('Location: ' . $url, true, $matches[1]);