Laravel - Using (:any?) wildcard for ALL routes?

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Mastering Laravel Routing with Wildcards for Versatile URL Handling Introduction: Routing is an essential part of web applications, especially in the rapidly evolving world of technology. In this blog post, we will explore how to use wildcard routing for flexibility and convenience while handling different routes in your Laravel application. We'll cover various approaches, best practices, and provide working examples to help you create efficient routes. The Problem: You're building a content management system (CMS) using Laravel and need two primary routes - one for the administrative dashboard at '/admin', and another for all other pages under the root path ('/'). The problem arises when trying to implement the second route with wildcard routing. With a '(:any)', it works only if you add something after '/' but doesn't work otherwise. You also want to handle a large set of routes like '/', '/something', and so on, leading to your 'view@index' controller. Solution 1: Using Wildcard Routing with Explicit Ordering To solve this routing problem, you can utilize the explicit ordering of your routes. Laravel follows an order for processing route requests. You can use this behavior to direct every request that doesn't match the 'admin' controller to the 'view' controller through wildcard routing. Here's how: Step 1: Register your 'admin' and 'view' controllers in the respective routes files. Step 2: Add a default route after your existing ones (with admin and view controllers). - This will ensure that all other requests are evaluated when not matching any of the prior defined routes. ```PHP Route::get(array('admin', 'admin/dashboard'), array( 'as' => 'admin', 'uses' => 'admin.dashboard@index' )); Route::view('/', 'view'); // Your existing view@index route may look like this: Route::any('(:any)', 'view@index'); // New route to handle all other requests under the root path: Route::defaultController(function ($request) { $controller = Route::getRoutes()->match($request)->getName(); if (!$controller) { return redirect('/admin'); } return Redirect::to("/$controller"); }); ``` Solution 2: Using RegEx to Match Specific Paths Alternatively, you can use regular expressions (RegEx) to handle specific paths with greater precision. By defining a custom route group and using the 'where' function, you can create a set of rules for routing requests accordingly: Step 1: Add your custom route group at the bottom of your routes file. ```PHP Route::group(array('prefix' => ''), function() { // Custom path rules here }); ``` Step 2: Define a set of rules for handling specific paths like '/', '/something/etc./'. ```PHP // For all requests under the root path, send them to index@view controller Route::get('/', 'view@index'); // For non-empty paths with any number of segments, use a regular expression to match them Route::post('{any_segment:.*}', function ($segment) { return Redirect::to("/$segment"); }); ``` Conclusion: In this post, we've explored different approaches for handling wildcard routing in Laravel applications. While solution 1 is easier to implement, solution 2 offers more precise control over your routes. Ultimately, the most suitable method depends on your specific application needs and requirements. Always remember to keep your routes well-organized, concise, and easy to maintain for better code readability and project scalability.