Skip to content

Commit

Permalink
IONOS: feat(base): add "redirect" config
Browse files Browse the repository at this point in the history
The "redirects" config allows configuring redirects from paths to routes.

Example config:

   "redirects" => { "^/path/regexp" => "an.apps.controller.route.locator" }

Use case:

* An administrator can configure another app as target for certain paths.

Differentiation from Apache redirects:

* this allows configuring redirects in one place and avoids spreading
  the configuration in multiple places.

* the target route locator is more expressive/easier to trace than a
  path

Signed-off-by: Thomas Lehmann <[email protected]>
  • Loading branch information
thlehmann-ionos committed Jul 5, 2024
1 parent b066e24 commit db33426
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
19 changes: 19 additions & 0 deletions config/config.sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -2438,4 +2438,23 @@
* Defaults to ``true``
*/
'enable_non-accessible_features' => true,

/**
* Request path without /index.php/ maps to a controller path in the form
* <app name>.<controller name>.<handler>.
*
* - For a FooController.php the controller name is "foo" (lowercase)
* - A handler would be a method in FooController that was annotated with
* - either #[FrontpageRoute] attribute
* - or configured in routes.php
*
* Defaults to ``[]`` (no redirects)
*/
'redirects' => [
/**
* Example:
* '^\/settings' => 'acmesettings.page.index'
*/
],

];
23 changes: 22 additions & 1 deletion lib/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
* @author Tobia De Koninck <[email protected]>
* @author Vincent Petry <[email protected]>
* @author Volkan Gezer <[email protected]>
* @author Thomas Lehmann <[email protected]>
*
* @license AGPL-3.0
*
Expand Down Expand Up @@ -1047,7 +1048,27 @@ public static function handleRequest(): void {
OC_App::loadApps(['filesystem', 'logging']);
OC_App::loadApps();
}
Server::get(\OC\Route\Router::class)->match($request->getRawPathInfo());
$requestPath = $request->getRawPathInfo();
$redirects = $systemConfig->getValue('redirects', []);

if ($redirects) {
foreach ($redirects as $fromPattern => $toLocator) {
if (!preg_match('/' . $fromPattern . '/', $requestPath)) {
continue;
}

try {
$targetLocation = Server::get(IURLGenerator::class)->linkToRouteAbsolute($toLocator);
header('Location: ' . $targetLocation);
return;
} catch (\Exception) {
// In case of container exceptions or
// route not found exceptions we proceed as usual.
}
}
}

Server::get(\OC\Route\Router::class)->match($requestPath);
return;
} catch (Symfony\Component\Routing\Exception\ResourceNotFoundException $e) {
//header('HTTP/1.0 404 Not Found');
Expand Down

0 comments on commit db33426

Please sign in to comment.