But we’ve moved on from dirt roads to cobblestone to asphalt. Let’s modernize our Laravel routes and make the old ones deprecated, not broken!
I had a few route that needed a little modification, but I knew people bookmarked it and Google is indexing daily so I wanted to keep it yet deprecate it with a decent HTTP 301 Moved Permanently redirect status.
My old website had URL’s like the following:
- mysite.com/news/46/a%20generic%20article%20about%20html
- mysite.com/news/104/url%20encoding%20explained
It was an easy, but ugly solution against duplicate titles plus horrible URL encoding result, so I figured out a way to make the titles more descriptive and unique and since the new site was made with Laravel I came up with the following:
// The new route Route::get('{seoname}', array('as' => 'article', 'uses' => 'NewsController@getArticle')); // The old route reinvented in Laravel routing Route::get('news/{id}/{title}', function($id){ $article = new Article($id); Redirect::route('article', $article->seoname, 301); });
The result is that if you go to mysite.com/news/46/a%20generic%20article%20about%20html
you automatically get redirected to mysite.com/a-beginners-introduction-to-html
in a blink of an eye.