简体   繁体   English

Laravel 5.5中的博客文章URL重定向

[英]Blog post URL redirection in laravel 5.5

Recently we included blog title along with ID in blog URL. 最近,我们在博客URL中包含了博客标题和ID。 For example, The old URL: 例如,旧的URL:

www.domain.com/blog-details/12

The modified URL: 修改后的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. 现在,我想将旧博客URL重定向到laravel网站中已修改的博客URL,如果我们单击旧博客URL,则应将其重定向到新博客URL。

You need to create a route to handle www.domain.com/blog-details/12 , fetch the right blog title and then redirect. 您需要创建一条路由来处理www.domain.com/blog-details/12 ,获取正确的博客标题,然后重定向。

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 https://laravel.com/docs/5.8/redirects

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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