Laravel 将路由从index.php?id={id}重定向到/index/{slug}

Laravel 将路由从index.php?id={id}重定向到/index/{slug},laravel,laravel-5.5,laravel-routing,Laravel,Laravel 5.5,Laravel Routing,我将一个旧项目转换为laravel,我想创建一些301路由来重定向旧url 旧url:example.com/index.php?id={id} 新url:example.com/index/{slug} 我试着做一些类似的事情: Route::get('index.php?id={id}', function($id){ $slug = {where I select slug from id} return Redirect::to('/index/'.$slug, 301)

我将一个旧项目转换为laravel,我想创建一些301路由来重定向旧url

旧url:example.com/index.php?id={id}

新url:example.com/index/{slug}

我试着做一些类似的事情:

Route::get('index.php?id={id}', function($id){
    $slug = {where I select slug from id}
    return Redirect::to('/index/'.$slug, 301);
});

但是它不起作用,url不可识别。您的做法有点错误,您不需要路由,而是需要一个中间件

php artisan make:中间件重定向

public function handle( $request, Closure $next )
{
    if( preg_match( '#index.php\?id=(\d+)#is', $request->fullurl(), $matches ) ) {
        return redirect()->to( "/index/{$matches[1]}", 301 );
    }
    return $next( $request );
}

将中间件添加到Http内核

你所说的{query}是什么意思??你是说要传递哪个id?抓取slug Var的查询没有得到你的答案。这不相关,问题是url不可识别,而不是我用来获取通过id的slug变量的查询受保护$middleware中的错误是什么?我设置为最佳答案,但regex不适用于我的情况,我必须更改为:if(preg_match)('/index.php\?id=(\d+/”,$request->fullurl(),$matches)){(谢谢!)我没有时间测试。我已经更新了答案。