Don’t get me wrong, I think that Contao is a great system, it’s just some of its functions are kinda counterintuitive and search engine is one of them. Furthermore it’s not easy to find clear information as to how it works in later versions of the CMS (in this case I used Contao 2.10.1).
So you have a Contao website that needs search and you know Contao has built-in search engine, you just have to “turn it on”. You check out the official docs and see that there is only one module called Search engine that “Adds a search form to a page”.
Sounds simple, you create the module, put it into your layout or article, go to your website and voila, it works. One problem though, search results are shown right near your search box, which is in most cases not what you want. What you probably want, is a search box on all your pages and a search results page that shows, well, search results – that’s what Google, or say WordPress do.
Obviously Search engine module doesn’t just add a search form to a page, it also shows results, so what we need to do is manually divide the “form showing” and “results showing” parts. Here is how it’s done:
- As already said – create a new module of the type Search engine, name it “Search Box” or something like that, add an id or a class if you need them.
- Create another module of the same type and name it “Search Results”.
- Add the “Search Box” module to your layout wherever you want the search form to appear.
- Now create a new page that will show the search results, make sure to check “Hide from navigation” under expert settings. Add an article here with “Search Results” module in it.
- Go back and edit the “Search Box” module, set the Redirect page to your search results page. Just in case: make sure the “Search Results” module doesn’t redirect your users anywhere else and don’t set Reference page unless you only want to search some part of your website.
That’s it, now you should have the search running. “Search Box” module will redirect the user to your search results page attaching search query as GET parameter where your search results module will show the actual results as well as pre-filled search form in content area. Make sure to use “indexer::stop” and “indexer::continue” HTML comments to exclude repeating areas of your content from the search index.
Thx man! Great tutorial! I use Contao 3.5.25 and it’s also work 🙂 Thx for help!
I’m glad it helped, thanks for the feedback!