Reputation: 53
i'm planning to change all my website URls .. i made controller and model to save all old and new url in my DB
and add this in my web.php
Route::group(['namespace'=>'Redirections'],function(){
$redirections=App\Models\Redirections::all();
foreach($redirections as $r){
Route::redirect($r->from,$r->to,$r->type, 301);
}
});
my project link is https://www.website.com/new-laravel
old link https://www.website.com/new-laravel/old
new link https://www.website.com/new-laravel/old
my problem redirect send me to https://www.website.com/old
How i can fix this problem
Upvotes: 0
Views: 1994
Reputation: 10210
I don't think putting your redirects into a database table is the best approach. You're going to have the overhead of a database call for each redirect vs just defining your redirects in the web.php
route file.
If your redirects are as straight forward as your question is suggesting, adding new-laravel
after your domain root, you could do the following in your web.php
route file.
// 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 () {
...
});
});
So trying to access website.com/old
will redirect to website.com/new-laravel/old
without the overhead of a database call.
Upvotes: 1