diff --git a/redirect.php b/redirect.php index 7d69983..81d5100 100644 --- a/redirect.php +++ b/redirect.php @@ -42,38 +42,74 @@ STR; } } -// Redirect the user according to found information -$name = urldecode($_SERVER['REQUEST_URI']); -$STATEMENTS['LookUp']->bind_param('s', $name); -if(!$STATEMENTS['LookUp']->execute()) { - header("Service Unavailable", true, 503); - die($SQL['Handle']->errno." ".$SQL['Handle']->error); -} +{ // Try and redirect user according to the full provided URI + $url = urldecode($_SERVER['REQUEST_URI']); + $url_parts = parse_url($url); -// -$res = $STATEMENTS['LookUp']->get_result(); -if ($res->num_rows == 0) { - // Insert the undiscovered URL into the redirect database. - $STATEMENTS['Insert']->bind_param('s', $name); - if (!$STATEMENTS['Insert']->execute()) { + // Try and identify a redirect by the full URI. + $STATEMENTS['LookUp']->bind_param('s', $url); + if (!$STATEMENTS['LookUp']->execute()) { header("Service Unavailable", true, 503); + header("Retry-After: 30", true); die($SQL['Handle']->errno." ".$SQL['Handle']->error); } else { - if (!$STATEMENTS['Insert']->get_result()) { - header("Service Unavailable", true, 503); - die($SQL['Handle']->errno." ".$SQL['Handle']->error); - } - } - header("Location: https://www.xaymar.com/404.html"); -} else { - while($obj = $res->fetch_object()) { - if ($obj->target != null) { - header("Location: ".$obj->target, true, 307); + $res = $STATEMENTS['LookUp']->get_result(); + if ($res->num_rows != 0) { + while($obj = $res->fetch_object()) { + if ($obj->target != null) { + header("Location: ".$obj->target, true, 307); + $res->close(); + die(); + } + } } else { - header("Location: https://www.xaymar.com/404.html"); + $STATEMENTS['Insert']->bind_param('s', $url); + if (!$STATEMENTS['Insert']->execute()) { + header("Service Unavailable", true, 503); + header("Retry-After: 30", true); + die($SQL['Handle']->errno." ".$SQL['Handle']->error); + } else if (!$STATEMENTS['Insert']->get_result()) { + header("Service Unavailable", true, 503); + header("Retry-After: 30", true); + die($SQL['Handle']->errno." ".$SQL['Handle']->error); + } } + $res->close(); } + + // Try and identify a redirect by the path. + $STATEMENTS['LookUp']->bind_param('s', $url_parts->path); + if (!$STATEMENTS['LookUp']->execute()) { + header("Service Unavailable", true, 503); + header("Retry-After: 30", true); + die($SQL['Handle']->errno." ".$SQL['Handle']->error); + } else { + $res = $STATEMENTS['LookUp']->get_result(); + if ($res->num_rows != 0) { + while($obj = $res->fetch_object()) { + if ($obj->target != null) { + header("Location: ".$obj->target, true, 307); + $res->close(); + die(); + } + } + } else { + $STATEMENTS['Insert']->bind_param('s', $url_parts->path); + if (!$STATEMENTS['Insert']->execute()) { + header("Service Unavailable", true, 503); + header("Retry-After: 30", true); + die($SQL['Handle']->errno." ".$SQL['Handle']->error); + } else if (!$STATEMENTS['Insert']->get_result()) { + header("Service Unavailable", true, 503); + header("Retry-After: 30", true); + die($SQL['Handle']->errno." ".$SQL['Handle']->error); + } + } + $res->close(); + } + + // In all other cases, just redirect to 404. + header("Location: https://www.xaymar.com/404.html"); } -$res->close(); die(); \ No newline at end of file