2

I can't find a way to clean up the search address from /search/?q=.../ to /search/query/.

I see ways of getting the last segment of the url with something like craft.request.getSegment. My other permalinks work fine, but for search I can't get the search template to pickup the /query/ bit (results in a logical 404 page).

So either it's my .htaccess (using the one provided) missing something. Or alter something else.

Thanks for any help!

5

You forgot to setup the routes for your search URLs.

In this case I’d recommend to use an advanced route with a regular expression, as it allows to use patterns that will ignore the slashes between your search terms. You’d setup the route in your craft/config/routes.php file (see Advanced Routing in the Craft docs).

return array(
    'search/(?P<query>.+)' => 'search/_result',
);

This route matches all URLs which start with search/, populates a variable named query with everything that comes after the search/ and routes the request to a template search/_result.html.

In your template you can now use the split filter to get the individual words. You can then join them to a nice search string for your criteria model (see Search Syntax in the docs).

{% set words = query|split('/') %}
{% set searchParam = words|join(' OR ') %}

Using this Twig code you’d prepare a search string that finds elements containing “advanced” or “routes” for an example URL http://example.com/search/advanced/routes.

| improve this answer | |
  • Thanks, this worked like a charm! Learned something new :) – Kitsune Dec 8 '16 at 19:50
3

If you go to Settings > Routes in the CP you're able to add routes with wildcard characters like:

/search/*

and then point that to a template.

You can also register site routes in a plugin if you want to go down that route (no pun intended)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.