Route redirect '/' to '/v1'


Hello, i have some routes like these:

$app->group('/v1', function () {
      $this->get('/something', $callback);
      $this->get('/something2', $callback2);

Now, how can i redirect all routes without version prefix to ‘v1’, Example:
$app->redirect('/', '/v1')

Thank you.


There are a few ways you could go about this, depending on what you are trying to achieve. Personally, I would create these type of redirect rules within my webserver’s config. If using Apache for example, I’d setup the .htaccess with a RewriteRule to implement the redirect.

You could also setup another route group that captures those requests and then uses Slim’s withRedirect method. In this instance you need to specify exactly where to redirect to, so you might need to work this out from the original request.


Try this:

($container->request->getUri()->getPath() == "/") ? $app->redirect('/', '/v1/', 301) : null;