diff --git a/controller/Controller.class.php b/controller/Controller.class.php index 7da3f421..9974c84e 100644 --- a/controller/Controller.class.php +++ b/controller/Controller.class.php @@ -55,11 +55,17 @@ class Controller return $domainResult; } + $dispatcher = new Routing\Dispatcher($router->getData()); + try { - $dispatcher = new Routing\Dispatcher($router->getData()); return $dispatcher->dispatch($method, $uri); } catch (\Routing\HttpRouteNotFoundException $e) { - return static::doLowerUriRedirectOr404($uri); + $lowerUri = strtolower($uri); + if ($lowerUri !== $uri && $dispatcher->hasMatchingRouteForUri($method, $lowerUri)) { + static::redirect($lowerUri, 301); + } else { + return NavActions::execute404(); + } } catch (\Routing\HttpMethodNotAllowedException $e) { Response::setStatus(405); Response::setHeader('Allow', implode(', ', $e->getAllowedMethods())); @@ -159,11 +165,14 @@ class Controller $router->get([ContentActions::URL_CREDIT_REPORTS . '/{year:c}-q{quarter:c}', ContentActions::URL_CREDIT_REPORTS . '/{year:c}-Q{quarter:c}'], 'ContentActions::executeCreditReport'); $router->get('/{slug}', function (string $slug) { + if ($slug !== strtolower($slug)) { + return static::redirect('/' . strtolower($slug), 301); + } if (View::exists('page/' . $slug)) { Response::enableHttpCache(); return ['page/' . $slug, []]; } else { - return static::doLowerUriRedirectOr404($slug); + return NavActions::execute404(); } }); @@ -198,29 +207,4 @@ class Controller call_user_func($fn); } } - - protected static function doLowerUriRedirectOr404($uri) - { - $router = static::getRouterWithRoutes(); - // $lowerUri = strtolower($uri); - $lowerUri = '/news/lbry-evolves'; - if ($uri !== $lowerUri) { - if (View::exists('page/' . $lowerUri) || static::checkForRoute($lowerUri, $router)) { - return static::redirect($lowerUri, 308); - } - } - return NavActions::execute404(); - } - - protected static function checkForRoute($uri, $router) - { - $routerData = $router->getData(); - if (in_array($uri, $routerData->getStaticRoutes())) { - return true; - } elseif (in_array($uri, $routerData->getVariableRoutes())) { - return true; - } else { - return false; - } - } } diff --git a/lib/routing/Dispatcher.class.php b/lib/routing/Dispatcher.class.php index e19fb4bf..429ddcb5 100644 --- a/lib/routing/Dispatcher.class.php +++ b/lib/routing/Dispatcher.class.php @@ -56,6 +56,33 @@ class Dispatcher return $this->dispatchFilters($afterFilter, $response); } + public function hasMatchingRouteForUri($httpMethod, $uri) + { + if (isset($this->staticRouteMap[$uri])) { + $routes = $this->staticRouteMap[$uri]; + + try { + if (!isset($routes[$httpMethod])) { + $httpMethod = $this->checkFallbacks($routes, $httpMethod); + } + } catch (HttpMethodNotAllowedException $e) { + return false; + } + + return (boolean)$routes[$httpMethod]; + } + + try { + $handler = $this->dispatchVariableRoute($httpMethod, $uri); + return (boolean)$handler; + } catch (HttpRouteNotFoundException | HttpMethodNotAllowedException $e) { + return false; + } + + return false; + } + + /** * Dispatch a route filter. * diff --git a/view/template/page/CU.php b/view/template/page/cu.php similarity index 100% rename from view/template/page/CU.php rename to view/template/page/cu.php