Skip to main content

December 16, 2014

Creating Custom Search Pages with Search404 and Apache Solr Search

Thought by Artem Dotsenko, Drupal Developer

Imagine somewhere deep in your site that you have a page with the alias:


Now, let’s imagine this page is not a link in your site’s menu. Your visitor remembers that they have seen this page on your site and starts typing in the address line:

What will they get? They may get a 404 error page or perhaps a sitemap with a lot of links which could confuse the user. What is their most likely path at this point? Will they start searching interesting pages in the sitemap or just go away and Google it? Answer: Neither.

Chances are, your visitor will leave your site and may not ever come back. Wouldn’t it be nice if on ‘page not found’ your visitors could search the words in the path ‘how to make a drupal site’ and it could present search results with the intended result at the top? Meet Search404.

This post is related to one of the Top 100 Drupal Modules (as on Sep 9, 2012) Search 404 and its work with Apache Solr Search on a Drupal 7 base.

First we’re going to assume that you have installed and configured Apache Solr Search and is working on your site.  The next step will be to download and enable the Search 404 module. After that, you'll be ready to configure their work as partners.

Step One. Site page 404 settings

On Configuration -> System -> Site information page set default 404 (not found) page to search404.

Step Two. New Apache Solr Search page

On Apache Solr Search configuration page (Configuration -> Search and metadata -> Apache Solr search) go to PAGES/BLOCKS tag.

And add a new search page with desired settings.

Step Three. Add search page

Make sure Path in your new search page is different from default 404 site page, not equal to search404 . Also use a custom filter to filter content types that are used as pages on your site. Custom filter must be like bundle:{content_type} without braces.

Step Four. Search404 settings

Go to Configuration -> Search and metadata -> Search 404 settings. Enable Do a "Search" with custom path instead of a Drupal Search when a 404 occurs checkbox and fill Custom search path with your Apache Solr search page, created on previous step, and /@keys argument.

The rest of the settings can be set based on required behavior.

Ok, we’re almost all done.

Step Five. Status 404 Not Found

For this moment you must have a search404 not found page that will make search with Apache Solr search. But this page won’t set page status 404 ( That is why it was written ‘almost all done’. To do this you will need to add some code. You can use hook_preprocess_html_()  to set page 404 status, like in an next example:

function MODULE_preprocess_html(&$variables) {

 // ‘page-not-found’ path was set in step two.

 if (arg(0) && arg(0) == ‘page-not-found’) {

   drupal_add_http_header('Status', '404 Not Found');



or you can use your option to set status 404.

Now we’re all done and you have a 404 page with search results gathered by keywords from the wrong link that the visitor originally visited.