<p>If you go to <code>Settings &gt; Routes</code> in the CP you're able to add routes with wildcard characters like:</p>

<pre><code>/search/*
</code></pre>

<p>and then point that to a template.</p>

<p>You can also <a href="https://craftcms.com/docs/plugins/hooks-reference#registerSiteRoutes" rel="nofollow noreferrer">register site routes</a> in a plugin if you want to go down that route (no pun intended)</p>

CC BY-SA 3.0
2016-12-08T09:52:54.963Z
2016-12-09T08:41:21.887Z
2016-12-09T08:41:21.887Z
KnutSv
<p>You forgot to setup the routes for your search URLs.</p>

<p>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 <a href="https://craftcms.com/docs/routing#advanced-routing" rel="nofollow noreferrer">Advanced Routing</a> in the Craft docs).</p>

<pre class="lang-php prettyprint-override"><code>return array(
    'search/(?P&lt;query&gt;.+)' =&gt; 'search/_result',
);
</code></pre>

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

<p>In your template you can now use the <code>split</code> filter to get the individual words. You can then join them to a nice search string for your criteria model (see <a href="https://craftcms.com/docs/searching" rel="nofollow noreferrer">Search Syntax</a> in the docs).</p>

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

<p>Using this Twig code you’d prepare a search string that finds elements containing “advanced” or “routes” for an example URL <a href="http://example.com/search/advanced/routes" rel="nofollow noreferrer">http://example.com/search/advanced/routes</a>.</p>

CC BY-SA 3.0
2016-12-08T19:50:56.167Z
Thanks, this worked like a charm! Learned something new :)
Kitsune
CC BY-SA 3.0
2016-12-08T09:55:32.563Z
2016-12-08T09:55:32.563Z
carlcs
<p>I can't find a way to clean up the search address from
<code>/search/?q=.../</code> to <code>/search/query/</code>.</p>

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

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

<p>Thanks for any help!</p>

<search><routes>
Prettify search URL
# Prettify search URL

(Asked by: username_0 on 2016-12-08)

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!

**Tags:** search, routes


---

## 2 answers

### Answer by username_2 on 2016-12-08. Score: 5 | Accepted answer

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.+)' => '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.

> **Comments:**
> **username_0**: Thanks, this worked like a charm! Learned something new :)  

---

### Answer by username_1 on 2016-12-08. Score: 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)


---

