How to redirect to admin page with except routes

Hello, how do I achieve in October CMS in my plugin a routing system that redirects various URLs to /admin, i.e., to the admin panel, when entered? However, it should not redirect if it is an API route or the direct /admin address?

// Redirect all other routes to the /admin page except api endpoints
Route::any('{any}', function($any) {

    if (preg_match('#^api/lakemanagement|admin#', $any)) {
        return;
    }
    
    return Redirect::to('/admin');
    
})->where('any', '.*');

Hey @Dingo497

If you want to redirect everything except API endpoints, I would create a theme that has one page with the following content inside

{% do redirect('/admin') %}

Then, I would enable Maintenance Mode via settings and select that page. Then inside the config/cms.php file, I would add an exception for API routes for maintenance mode.

'url_exceptions' => [
    '/api/*' => 'maintenance',
],
1 Like