如何在laravel上使用page.php?id = {id}路由

时间:2017-06-05 11:28:26

标签: php laravel redirect routes

我正在用laravel重写一个网站,为了SEO,我必须创建一些路由,以便在访问者访问网站的旧式链接时执行301重定向:

使用Laravel 5.4 + PHP 7.0

例如:
旧链接:www.website.com/category.php?category = {id}
将重定向到:
新链接:www.website.com/category/{category-name}

但是当我在laravel上创建这样的路线时,它不起作用:

Route::get('/category.php?category={id}', function ($id) {
 // Old route style, search new route and do the redirect.
});

如果我删除“?”它有点工作,但它不会针对我的旧链接,因为没有“?”在网址中。

是否支持laravel?
如果不是,那么实现这一目标的好方法是什么?

3 个答案:

答案 0 :(得分:2)

制作这样的路线并传递类别ID。例如,我已经采取了那个10.

<a href="www.website.com/category.php?category=10">button</a>

并从请求对象中获取id。

Route::get('/category.php', function (\Illuminate\Http\Request $request) {
    $id = $request->category;
    // Old route style, search new route and do the redirect.
});

答案 1 :(得分:0)

在这种情况下,Htaccess是你的朋友。您可以创建从旧类别库到新类别的301重定向,如下所示:

RewriteEngine on
RewriteBase /
RewriteRule ^category.php?category=(.*) http://www.website.com/category/$1 [R=301,L]

答案 2 :(得分:0)

你的控制器是什么? 您可以获取控制器资源 并且所有这些路线都将被添加到您的路线:没有痛苦的列表。

Route:: resource ('category', 'controller');