I have routes like this coming from the database
$cat_routes = App\User::list_routes();
foreach ($cat_routes as $route){
Route::get('/category/{'.$route->route.'}', CategoriesController@getCategoryByRoute');
}
To access this category the URL will be:
domain.com/category/cars (or any category rather than cars)
Is there anyway to create custom URL or slug to change the URL like this:
domain.com/cars (clothes, women, watches .... etc)
So when the user clicks a link like this "domain.com/category/cars" he gets redirected to "domain.com/cars" and the controller keeps handling it as a category "category/cars".
The function looks like this:
public function getCategoryByRoute($category_route)
Can this be done from Laravel or htaccess?
Note that I have other short URLs like
domain.com/gallery
domain.com/login
......
So I don't want to redirect or shorten all URLs. Just the category URLs.
Part of your answer was about redirecting. Instead of .htaccess, you could do it in Laravel itself. Better, because if you move to Nginx (or something else), you dont have to worry about the "application logic in the htaccess"...
Route::get('/category/{categorySlug}', function($categorySlug) {
$redirectPath = sprintf('/%s', $categorySlug);
return redirect($redirectPath);
})->where('categorySlug', '[a-z0-9-]+');
Explanation:
Obviously, you need extra Routes to catch those redirects...
Route::get('/{categorySlug}', 'CategoriesController@getCategoryByRoute');
BTW, the default redirect status in this solution is 302 (Moved Temporarily). If you want to have a 301 (Moved Permanently):
redirect($redirectPath, 301);