Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bwrigley's avatar

passing search parameters as url path instead of query string

Hi all,

This must have been asked thousands of times but I'm not sure why my searching isn't coming up with anything.

I'd like to be able to pass search parameters as part of the url path instead of as a query string. So these are all valid:

/posts
/posts/user/someusername
/posts/tag/sometag
/posts/user/someusername/tag/sometag
/posts/tag/sometag/user/someusername

I was hoping for something like this:

Route::get('/posts/*', [PostController::class, 'index'])->name('feed');

Then parse in index() something like:

    public function index(Request $request):  Response
    {
		$params = $request->route()->parameters();
		///
	}

Can someone point me in the right direction?

0 likes
12 replies
s4muel's avatar

first you need to allow forward slash using the where on a route in a parameter (https://laravel.com/docs/10.x/routing#parameters-encoded-forward-slashes) like so:

Route::get('posts/{params}', function ($params) {
    dd($params);
})->where('params', '.*');

and then you can explode the segments by a forward slash and assign the params respetively. you need to come up with the solution yourself, but should be pretty straightforward, something like

//for /posts/user/234/tag/obsolete
str($params)->explode('/')->chunk(2)->toArray();
/*
[
    ['user', '234'],
    ['tag', 'obsolete'],
]
*/

you can build on that, just do not forget to validate the parameter names and values

1 like
Snapey's avatar

You would also need to ensure that characters that are not valid in a URL cannot be entered as search string

For instance, you could not search for "Brown & sons"

1 like
bwrigley's avatar

@Snapey Thank you!

Actually in this instance I'm not allowing an open search, I'll only be offering things like 'username' and 'tag' which would need to match an existing entry in the database.

martinbean's avatar

I'd like to be able to pass search parameters as part of the url path instead of as a query string.

@bwrigley Why? This is the exact purpose of query strings: to manipulate results in a listing.

Using the path is just going to over-complicate your URLs and also probably incur duplicate content penalties from search engines.

1 like
bwrigley's avatar

@martinbean thank you! I'd be really happy to be wrong on this but I thought I had understood the opposite for SEO?

I had thought that the engines generally don't like query strings but do like more structured URLs which is why CMS like wordpress tend to create URLS like /category/somecategory or /tag/sometag? I could be a bit stuck in the distant past on that though :)

I'd also quite like to offer users a 'pretty' shareable link to their feed, but I could find a different way to do that.

Thanks again for your time on this!

jlrdw's avatar

@bwrigley here is a Google query string: https://support.google.com/optimize/answer/6301786?hl=en#zippy=%2Cin-this-article

You are confusing a page (no query yet) with a page that has results.

Another : https://store.google.com/product/pixel_8?hl=en-US

The idea is have main page like: https://about.google/products/ a pretty url.

But query results (like list of products) be a query string.

I.e.:

https://workspace.google.com/products/forms/?hl=en&utm_source=driveforwork&utm_medium=et&utm_content=forms&utm_campaign=body

I suggest you maybe accept we know what we are talking about with query strings.

1 like
martinbean's avatar

@bwrigley I’m not saying rewrite your site to use query string URLs like ?pageid=1. But if you have a posts listing page, and you want a user to filter that list of posts using one or more criteria, then that’s when you’d use a query string like /posts?user=john-doe&tag=foo.

Search engines will understand query strings like the above (as they will most likely do some normalisation their side), but they will see URLs like /posts/author/john-doe/tag/foo and /posts/tag/foo/author/john-doe as two separate URLs but with the same content, and treat it as duplicate content.

1 like
bwrigley's avatar

@jlrdw thank you! I do indeed! I think I'm much more out of date than I realise! Thanks again.

jlrdw's avatar

To add, "pretty url's" is fine for a main site page. But for drill down results I also suggest the regular query string.

1 like
Snapey's avatar

If you are thinking about SEO, don't forget that search engines will not exercise your javascript and will not select the pages with different dropdowns selected.

Imagine how a search engine is going to crawl your site and approach it from that direction.

Yes, its perfectly possible to have variable segments in the URL.

Check my site here, and for instance, talks tagged "engineering". https://speakernet.co.uk/talks/tagged/engineering

or talks in the science category https://speakernet.co.uk/talks/category/6/science

or for speaker https://speakernet.co.uk/speaker/1029/david-lemon

or for individual talks https://speakernet.co.uk/talk/2597/the-african-elephant

Note here that the talk ID is present and used to find the relevant model, and the 'slug' for the talk is present for SEO only.

At no point though are search terms present in the response URL structure.

1 like
bwrigley's avatar

@Snapey thank you! yes that's similar (but better) that what I was looking at. A user would only be clicking on tag/username links in posts that would then filter the page, so nice easy links for search engines to follow.

Please or to participate in this conversation.