【问题标题】:Redirect route from index.php?id={id} to /index/{slug}将路由从 index.php?id={id} 重定向到 /index/{slug}
【发布时间】:2018-09-01 21:57:29
【问题描述】:

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

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

新网址: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);
});

但它不起作用,网址无法识别。

【问题讨论】:

  • 你所说的 {query} 是什么意思??你的意思是正在传递的 id ?
  • 获取 slug var 的查询
  • 没有得到你。
  • 不相关,问题是url无法识别,不是我用来获取slug变量传递id的查询
  • 错误是什么

标签: laravel laravel-5.5 laravel-routing


【解决方案1】:

你做的有点不对,你不需要一个路由,而是一个中间件。

php artisan make:middleware Redirect

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 Kernel

【讨论】:

  • 在受保护的 $middleware 中?
  • 我设置为最佳答案,但正则表达式不适用于我的情况我不得不更改为: if( preg_match( '/index.php\?id=(\d+)/', $request-> fullurl(), $matches ) ) { (谢谢!)
  • 我没有时间测试。我已经更新了答案,问候。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-01-26
  • 2017-06-02
  • 2015-08-11
  • 1970-01-01
  • 1970-01-01
  • 2016-07-28
  • 2016-01-10
相关资源
最近更新 更多