How to define a Laravel route with a parameter that contains a slash character

How to define a Laravel route with a parameter that contains a slash character

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Handling Slash Characters in Laravel Route Parameters

Laravel's routing engine treats forward slashes as delimiters by default. When you define a route like /view/{id}, the framework expects id to be a single segment of the URL path. If your application requires a parameter that inherently contains a slash—such as distinguishing between a product code like abc and a sub-region or version like 02 in a URL like example.com/view/abc/02—the standard syntax fails. Laravel interprets abc as the value for {id} and 02 as the start of the next route segment, often leading to a 404 error if no matching route exists for that second segment.

To resolve this, you must explicitly tell Laravel to treat the slash as part of the parameter's value rather than a path separator. This is achieved using the {parameter/regex} syntax or the {parameter/slug} modifier depending on your specific constraints, but the most robust method for arbitrary slashes is defining a custom regex pattern within the route definition itself.

You can define a route that accepts any characters following the slash by using a regular expression directly in the route parameter. For instance, if you want to capture everything after abc/, including subsequent slashes or specific patterns, you might define the route as follows:

Route::get('/view/{segment}', function ($segment) {
    // Logic here
})->where('segment', '([^/]+/[^/]+)');

However, a more flexible approach that allows for multiple slashes within the segment is to use a pattern that captures any string of characters:

Route::get('/view/{slug}', function ($slug) {
    // $slug will contain "abc/02"
})->where('slug', '.*');

While using .* seems simple, it can sometimes cause ambiguity if subsequent routes also start with a slash. A safer pattern for this specific scenario—one where you want to capture everything after the initial slash until the next major route segment—is often:

Route::get('/view/{slug}', function ($slug) {
    // $slug will be "abc/02"
})->where('slug', '.*');

If you need to ensure that the captured group includes slashes but still allows Laravel to distinguish where the parameter ends and the next potential route segment begins, you might need to adjust the global routing rules or use a specific regex that stops at a defined boundary. For example:

Route::get('/view/{slug}', function ($slug) {
    // $slug will be "abc/02"
})->where('slug', '.*');

Alternatively, if you are using named parameters and want to enforce a specific structure, you can use the regex option:

Route::get('/view/{slug}', function ($slug) {
    // $slug will be "abc/02"
})->where('slug', '.*');

One critical consideration is how this interacts with existing routes. If you have a route defined as /view/123, Laravel's routing priority might cause conflicts if the regex pattern for the slash-containing parameter is too broad. It is best practice to define more specific routes first and use catch-all patterns with caution. For example, ensure that a route expecting just an integer ID (/view/{id:int}) is defined before or handled distinctly from the slug-based route to avoid ambiguity.

Another approach involves using the {slug} placeholder if you are using Laravel 9 or later, which simplifies regex handling in some cases, but for older versions or specific complex needs, the where clause remains the definitive tool. You must ensure that the regex pattern does not inadvertently match other valid routes in your application stack.

If you require dynamic behavior where the slash is always present between two distinct components, consider restructuring your route model if possible. However, when restructure is not an option, the where clause with a permissive regex like .* or a specific pattern like [^/]+/[^/]+ provides the necessary flexibility without breaking the routing engine's parsing logic. Always test edge cases where the parameter might be empty or contain multiple slashes to ensure your application handles them gracefully.

Our network