37 lines
1.2 KiB
PHP
37 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Jinove\Vinoteca;
|
|
|
|
class Routing
|
|
{
|
|
private const MOVED = ["news" => "articles"];
|
|
|
|
public static function onPageStart(): void
|
|
{
|
|
if (defined("ADMIN_SECTION") || PHP_SAPI === "cli" || !in_array($_SERVER["REQUEST_METHOD"] ?? "", ["GET", "HEAD"], true)) {
|
|
return;
|
|
}
|
|
$uri = (string)($_SERVER["REQUEST_URI"] ?? "");
|
|
$path = (string)parse_url($uri, PHP_URL_PATH);
|
|
$query = (string)parse_url($uri, PHP_URL_QUERY);
|
|
if ($path === "" || preg_match('#^/(bitrix|upload|local|include)/#', $path)) {
|
|
return;
|
|
}
|
|
if (str_contains(basename($path), ".")) {
|
|
return;
|
|
}
|
|
$target = str_contains($path, "%") ? $path : mb_strtolower($path);
|
|
if (preg_match('#^(/(?:ru|pl))?/(' . implode("|", array_keys(self::MOVED)) . ')(/.*)?$#', $target, $match)) {
|
|
$target = $match[1] . "/" . self::MOVED[$match[2]] . ($match[3] ?? "/");
|
|
}
|
|
if (!str_ends_with($target, "/")) {
|
|
$target .= "/";
|
|
}
|
|
if ($target !== $path) {
|
|
LocalRedirect(Config::host() . $target . ($query !== "" ? "?" . $query : ""), true, "301 Moved permanently");
|
|
}
|
|
}
|
|
}
|