How to redirect to different domain in Slim framework of PHP?

3.5k Views Asked by At

Using Slim we can use routes like:

$app->get('/path', function() {
    include 'content.php';
});

We can also redirect to any other path of same domain like:

$app->get('/path2', function () use ($app) {
    $app->redirect('/redirect-here');
});

But I want to redirect to some different domain and none of below is working:

$app->get('/feeds', function(){
    $app->redirect('http://feeds.example.com/feed');
});

This shows blank page:

$app->get('/feeds', function() {
    header("Location: http://feeds.example.com/feed");
});
1

There are 1 best solutions below

1
On BEST ANSWER

In Slim 3, you should use the withRedirect method on the Response object:

$app->get('/feeds', function ($request, $response, $args) {
    return $response->withRedirect('http://feeds.example.com/feed', 301);
});

For Slim 2 only, you can do:

$app->get('/feeds', function() use ($app) {
    $app->redirect('http://feeds.example.com/feed');
});