使用 Slim 框架重定向

Redirect with Slim Framework

我有以下代码:

$app->get('/category/:name', function($name){
  //the render files are found under the templates folder
header('Location: searchPage.php?crs_category=$name');
});

问题是,当我键入 /category/business 时,它只会出现在空白页上。我不想呈现页面,因为我无法完全呈现 searchPage.php?crs_category,因为它被视为模板,并且有一个变通方法可以包含到变量中。我只想直接转到该页面,同时保持 url 干净。基本上在后台重定向到此页面,剩下 url。

提前致谢。

假设您正在使用 Apache 作为网络服务器,您不能只重写 .htaccess 中的 URL:

RewriteRule ^category/(.*)$ searchPage.php?crs_category= [L,NC,QSA]

..或者如果你想要一个真正的重定向

RewriteRule ^category/(.*)$ searchPage.php?crs_category= [L,NC,R=301]

PS如果你坚持在Slim中做,你可以试试

$app->get('/category/:name', function($name) use ($app) {
    $app->response->redirect('/searchPage.php?crs_category='.$name, 303);
});

来源:http://docs.slimframework.com/response/helpers/#redirect