skip to Main Content

I’m rewriting an website in laravel, and for the sake of the SEO, I have to create some routes that will do a 301 redirect in case the visitor get to access the old style links of the website:

Using Laravel 5.4 + PHP 7.0

For example:
Old links: www.website.com/category.php?category={id}
will redirect to:
New Link: www.website.com/category/{category-name}

But when I create a route like this on laravel, it doesn’t work:

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

If I remove the “?” it kinda works, but then it won’t target my old links, cause there’s no “?” in the url.

Is this supported on laravel ?
If it isn’t, what would be a good way to achieve this?

3

Answers


  1. Htaccess is your friend in this case. You can create a 301 redirect from your old category base to the new like so:

    RewriteEngine on
    RewriteBase /
    RewriteRule ^category.php?category=(.*) http://www.website.com/category/$1 [R=301,L]
    
    Login or Signup to reply.
  2. What is your controller?
    You can get your controller resource
    And all those route will be added to your route:list with no pain.

    Route:: resource ('category', 'controller');
    
    Login or Signup to reply.
  3. make your route like this and pass the category id. for example I have taken that 10.

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

    and take id from the request object.

    Route::get('/category.php', function (IlluminateHttpRequest $request) {
        $id = $request->category;
        // Old route style, search new route and do the redirect.
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search