简体   繁体   中英

Blog post URL redirection in laravel 5.5

Recently we included blog title along with ID in blog URL. For example, The old URL:

www.domain.com/blog-details/12

The modified URL:

www.domain.com/blog-details/12/title

Now I want to redirect the old blog URL to the modified blog URL in laravel website, if we click old blog URL, it should redirect to the new blog URL.

You need to create a route to handle www.domain.com/blog-details/12 , fetch the right blog title and then redirect.

If you're able to use model binding something like this should do the job

Route::get('/blog-details/{blog}', function (Blog $blog) {
    return redirect("/blog-details/$blog->id/$blog->title");
});

Otherwise you can fetch the blog item by yourself and then redirect

Route::get('/blog-details/{id}', function ($id) {
    $blog = Blog::findOrFail($id);
    return redirect("/blog-details/$blog->id/$blog->title");
});

You can read more about redirects here; https://laravel.com/docs/5.8/redirects

您可以将重写URL与.htacess Apache或.conf Nginx一起使用

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM