Udaya
Udaya

Reputation: 11

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.

Upvotes: 0

Views: 259

Answers (2)

Amando Vledder
Amando Vledder

Reputation: 1346

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

Upvotes: 2

Bon BN Le
Bon BN Le

Reputation: 1

You can use rewrite URLs with .htacess Apache or .conf Nginx

Upvotes: 0

Related Questions