mirror of
https://github.com/LBRYFoundation/lbry.com.git
synced 2025-08-23 17:47:26 +00:00
case insensitive urls
This commit is contained in:
parent
1d1c0be4fe
commit
f6c133abe4
3 changed files with 39 additions and 28 deletions
|
@ -55,11 +55,17 @@ class Controller
|
||||||
return $domainResult;
|
return $domainResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$dispatcher = new Routing\Dispatcher($router->getData());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$dispatcher = new Routing\Dispatcher($router->getData());
|
|
||||||
return $dispatcher->dispatch($method, $uri);
|
return $dispatcher->dispatch($method, $uri);
|
||||||
} catch (\Routing\HttpRouteNotFoundException $e) {
|
} 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) {
|
} catch (\Routing\HttpMethodNotAllowedException $e) {
|
||||||
Response::setStatus(405);
|
Response::setStatus(405);
|
||||||
Response::setHeader('Allow', implode(', ', $e->getAllowedMethods()));
|
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([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) {
|
$router->get('/{slug}', function (string $slug) {
|
||||||
|
if ($slug !== strtolower($slug)) {
|
||||||
|
return static::redirect('/' . strtolower($slug), 301);
|
||||||
|
}
|
||||||
if (View::exists('page/' . $slug)) {
|
if (View::exists('page/' . $slug)) {
|
||||||
Response::enableHttpCache();
|
Response::enableHttpCache();
|
||||||
return ['page/' . $slug, []];
|
return ['page/' . $slug, []];
|
||||||
} else {
|
} else {
|
||||||
return static::doLowerUriRedirectOr404($slug);
|
return NavActions::execute404();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -198,29 +207,4 @@ class Controller
|
||||||
call_user_func($fn);
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,6 +56,33 @@ class Dispatcher
|
||||||
return $this->dispatchFilters($afterFilter, $response);
|
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.
|
* Dispatch a route filter.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Add table
Reference in a new issue