<?php
// ============================================================
// MOBEL SUITE — SHORT ADDRESSES
// ------------------------------------------------------------
// .htaccess sends here any one-word address that is not a real file or folder
// (www.squirefurnishings.co.uk/divans). routes.php says what it is; this file
// opens that page as if it had been asked for by name, or the 404 page.
// ============================================================

if (!defined('BASE_DIR')) define('BASE_DIR', __DIR__ . '/');
require_once BASE_DIR . 'routes.php';

$_rt_path  = (string)parse_url((string)($_SERVER['REQUEST_URI'] ?? '/'), PHP_URL_PATH);
$_rt_query = (string)parse_url((string)($_SERVER['REQUEST_URI'] ?? '/'), PHP_URL_QUERY);

// /divans/ is /divans. The slash would make every relative link on the page
// resolve inside a folder that does not exist (see routes.php), so it is taken
// off with a permanent redirect rather than served.
if (strlen($_rt_path) > 1 && substr($_rt_path, -1) === '/') {
    header('Location: ' . rtrim($_rt_path, '/') . ($_rt_query !== '' ? '?' . $_rt_query : ''), true, 301);
    exit;
}

$_rt = site_route(ltrim(rawurldecode($_rt_path), '/'));
if (!$_rt) {
    require BASE_DIR . '404.php';
    exit;
}
// Upper case in an address is the same page; the address shown is the lower.
if (ltrim($_rt_path, '/') !== $_rt['route']) {
    header('Location: /' . $_rt['route'] . ($_rt_query !== '' ? '?' . $_rt_query : ''), true, 301);
    exit;
}

foreach ($_rt['get'] as $_rt_k => $_rt_v) { $_GET[$_rt_k] = $_rt_v; $_REQUEST[$_rt_k] = $_rt_v; }
$GLOBALS['site_route'] = $_rt;
unset($_rt_path, $_rt_query, $_rt_k, $_rt_v);
require BASE_DIR . $_rt['file'];
