Rewrite Search Query with segment


I’m facing a kirby/php problem related to the resulting query itself.
I set a form action that leads to a /search page.
The resulting url is currently shown as : /search?q=table
But I’d like the url to be rewritten like : /search/table

I also added a RewriteRule but this one won’t help much :

RewriteRule ^search/$ search?q=$1 [NC,L]

Anyone thinks of a simple solution ?

Thanks for any hint

You might want to try Kirby’s own routing feature:

Yes neat idea. Thank you. Will post the resolved tip.

Any news on this? :smiley:

Thanks for the hint.
My form is only submitted with the action="/search" , I don’t mention any query.
That’s why I was thinking the initial request was made through a http_build_query object.

@walkerbox I replied above your message :slight_smile:

I’m not sure I fully understand the problem. Maybe you can elaborate a bit because I might be missing something here as I don’t know what you mean by transforming http_build_query object.

You will need to override the submit event on your form in order for the browser to submit a pretty URL instead of the normal (standard) GET request you are seeing. The alternative would be to redirect the user whenever they submit the form from an ugly URL to a pretty one, but I think redirecting in that way is bad form when it’s your own interface that sent them there.

Once the user is submitting pretty URLs, handling them via kirby is simple with the router.

c::set('routes', array(
    'pattern' => 'search/(:all)',
    'action'  => function($q) {
      r::set('q', $q);
      return page('search');

This is assuming you are wanting specifically to use r::get('q') in your search page or controller. The benefit is that it will mean your script will still work for people submitting without javascript (and the pretty URLs) without needing any extra code.

Well, sorry for this late answer.
I actually avoided the problem and kept the standard query url. Thanks anyway for your help.