Php Laravel 8从旧URL重定向到新URL
我计划更改我所有的网站URL。。我制作了控制器和模型来保存数据库中的所有新旧url 并将其添加到my web.php中Php Laravel 8从旧URL重定向到新URL,php,laravel,seo,laravel-8,Php,Laravel,Seo,Laravel 8,我计划更改我所有的网站URL。。我制作了控制器和模型来保存数据库中的所有新旧url 并将其添加到my web.php中 Route::group(['namespace'=>'Redirections'],function(){ $redirections=App\Models\Redirections::all(); foreach($redirections as $r){ Route::redirect($r->from,$r->to,$r->
Route::group(['namespace'=>'Redirections'],function(){
$redirections=App\Models\Redirections::all();
foreach($redirections as $r){
Route::redirect($r->from,$r->to,$r->type, 301);
}
});
我的项目链接是
旧链接
新链接
我的问题请把我送到
如何解决这个问题我不认为将重定向放入数据库表是最好的方法。与只在
web.php
route文件中定义重定向相比,每个重定向都会有一个数据库调用的开销
// catch all routes that don't contain 'new-laravel'
Route::any('{all}', function (Request $request) {
return redirect(url('/new-laravel/' . $request->path()), 301);
})->where('all', '^((?!new-laravel).)*$');
// create a route group to wrap and catch all your old routes
Route::group('/new-laravel', function () {
Route::get('/old', function () {
...
});
});
如果您的重定向像您的问题所建议的那样直截了当,在域根之后添加new laravel
,那么您可以在web.php
路由文件中执行以下操作
// catch all routes that don't contain 'new-laravel'
Route::any('{all}', function (Request $request) {
return redirect(url('/new-laravel/' . $request->path()), 301);
})->where('all', '^((?!new-laravel).)*$');
// create a route group to wrap and catch all your old routes
Route::group('/new-laravel', function () {
Route::get('/old', function () {
...
});
});
因此,尝试访问
website.com/old
将重定向到website.com/new laravel/old
,而无需数据库调用。那么,您是否只是将new laravel
放在域根目录之后?你问题中的旧链接和新链接都是一样的。我有很多URL需要重定向,但都不一样,这就是为什么我要这么做。。。你能为我的案子提出其他建议吗?@AhmedSaad你不必把重定向放到数据库中。有时候根本就没有一个简单的解决办法。