Commit 32cab962 authored by Scott Zeid's avatar Scott Zeid

[static/redirect.php] Allow wildcard redirects; some small things

parent e2680c2f
......@@ -6,7 +6,11 @@
# Entries are of the format `<old-url> -> <new-url>`. Lines
# starting with a `#` are ignored as comments. Lines may be
# broken before and/or after the arrow. You may also use an
# equals sign instead of a hyphen.
# equals sign instead of a hyphen. If the old URL ends with
# `/*`, then it will be treated as a wildcard: the old URL
# followed by either a slash and extra parts or by nothing
# will be matched, and only the part of the old URL before
# the slash and asterisk will be replaced.
#
# This file is read by `_static/redirect.php`. To use it, tell
# your server to use that PHP script as the 404 handler. The
......
<?php
$request_uri = $_SERVER["REQUEST_URI"];
function redirect($to) {
header("Location: $to");
}
$redirects_text = file_get_contents("_redirects");
$redirects_text = preg_replace('/\s+[-=]>\s+/', ' => ', $redirects_text);
$redirects_text = str_replace("\r", "\n", str_replace("\r\n", "\n", $redirects_text));
......@@ -10,19 +15,25 @@ foreach ($redirect_lines as $line) {
if (strpos($line, "#") !== 0) {
$parts = explode(" => ", $line, 2);
if (!empty($parts[0]) && !empty($parts[1]))
$redirects[$parts[0]] = $parts[1];
$redirects[rtrim($parts[0], "/")] = $parts[1];
}
}
foreach ($redirects as $old => $new) {
if ($_SERVER["REQUEST_URI"] == $old ||
$_SERVER["REQUEST_URI"] == $old."/") {
header("Location: $new");
if (strlen($old) >= 2 && substr($old, strlen($old) - 2) === "/*" &&
(strpos($request_uri, substr($old, 0, strlen($old) - 1)) === 0 ||
$request_uri == substr($old, 0, strlen($old) - 2))) {
redirect(rtrim($new, "/") . substr($request_uri, strlen($old) - 2));
exit();
}
if ($request_uri == $old ||
$request_uri == $old."/") {
redirect($new);
exit();
}
}
if ($_SERVER["REQUEST_URI"] != $_SERVER["SCRIPT_NAME"]) {
if ($request_uri != $_SERVER["SCRIPT_NAME"]) {
header("HTTP/1.0 404 Not Found");
if (file_exists("404.html"))
readfile("404.html");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment