🎉 Try the public beta of the new docs site at algolia.com/doc-beta! 🎉
Guides / Managing results / Rules / Merchandising

Redirect searches to a URL

Search isn’t only about retrieving results. Sometimes, depending on what users search for, you might want to redirect them to a specific page of your website or mobile app.

Redirecting searches to a URL helps users in these cases:

  • Improve user experience and navigation. For example, searching for “help” or “return policy” redirects users to a support page.
  • Display category pages for related keywords to guarantee a consistent user experience and boost SEO by driving more traffic to category pages. For example, searching for a category keyword, such as, “TV” redirects users directly to the corresponding category page.
  • Promotional campaigns. For example, searching for a specific artist name or brand redirects users to a landing page created specifically to support a promotional operation.
  • Crisis management. Provide relevant answers to queries related to sudden events. For example, if you detect that your users suddenly start searching for “masks”, redirect them to a specific landing page.

Algolia lets you easily create redirects to specific URLs with the Autocomplete redirect plugin or a custom implementation

Algolia out-of-the-box redirect solution

The easiest way to redirect search queries to a URL is by adding a rule with the Visual Editor. The rule adds additional data to the search response, which you can handle in your frontend.

The examples on this page add a redirect for the keyword “help” to a support page. You can discover potential redirects by looking at your search analytics. For example, users searching for “help” and not getting any results.

Create a rule in the Visual Editor

  1. Go to the Algolia dashboard and select your Algolia application.
  2. On the left sidebar, select Algolia Search Search.
  3. Select the Algolia index to which you want to add a rule:

    Select your Algolia application and index

  4. Go to the Rules page.

  5. Click Create your first Rule or New rule and select Visual Editor.
  6. In the It all starts here section, click Set query conditions.
  7. In the Your query input, enter help and click Apply.
  8. In the What do you want to do? section, click Redirect.
  9. Enter the URL for your redirect—for example, the URL of your support page and click Apply.
  10. Confirm your changes by clicking Review and Publish.

Create a rule with the API

To add a rule with the API, use the saveRule method. When adding a rule, you need to define a condition and a consequence. In the consequence, add a search parameters and configure renderingContent using the following template:

1
2
3
4
5
6
7
{
  "renderingContent": {
    "redirect": {
      "url": "https://www.algolia.com/support"
    }
  }
}

Handle the redirect data in your user interface

After adding the rule, you can trigger the redirect in your UI when the renderingContent.redirect property is present in the API response. You have two options:

Extend the out-of-the-box approach or create custom redirection

If you want to define your own redirection format, or if you need to add additional attributes to the default redirect rule format, you can create a rule with the Manual Editor that returns custom data.

Create a rule for returning custom data

You can set a rule that contains the custom data you need to build your custom redirect.

This example redirects users to your support page whenever the query contains “help”.

Create a rule in the Manual Editor

  1. Go to the Algolia dashboard and select your Algolia application.
  2. On the left sidebar, select Algolia Search Search.
  3. Select the Algolia index to which you want to add a rule:

    Select your Algolia application and index

  4. Go to the Rules page.

  5. Click Create your first Rule or New rule and select Manual Editor.
  6. In the Condition(s) section, keep the Query option selected, and enter help in the input.
  7. In the Consequence(s) section, click Add consequence and select Return Custom Data.

  8. In the Custom JSON data input, add the data you want to return when the query matches the rule’s condition. For example if you want to create a complete custom redirection, enter the following JSON object: {"redirect": "https://www.algolia.com/support"}

  9. Optional: add a description for the rule, that helps you and your team distinguish it from other rules.
  10. Click Save.

Create a rule with the API

To add a rule with the API, use the saveRule method. When setting a rule, you need to define a condition and a consequence.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$rule = array(
  'objectID' => 'redirect-help-rule',
  'condition' => array(
    'pattern'   => 'help',
    'anchoring' => 'contains',
  ),
  'consequence' => array(
    'userData' => array(
      'redirect' => 'https://www.algolia.com/support'
    )
  )
);

$response = $index->saveRule($rule);

Handle the redirect data in your user interface

After adding the rule, you can trigger the redirect in your UI when the userData property is present in the API response. If you’re using one of the InstantSearch UI libraries, see Redirects in InstantSearch.

Add redirects using a dedicated index

You can set up redirects by creating a separate index with the list of redirects and the queries that trigger them. Whenever a user performs a search, you have to look in two indices: the regular index to search for items, and the redirect index that determines whether the user should be redirected.

This technique is useful if you can’t use Rules, or if you want to use other Algolia Search features with your redirect queries, such as typo tolerance, synonyms, or filters.

Dataset

1
2
3
4
5
6
7
8
9
10
[
  {
    "url": "https://www.google.com/#q=star+wars",
    "query_terms": ["star wars"]
  },
  {
    "url": "https://www.google.com/#q=star+wars+actors",
    "query_terms": ["george lucas", "luke skywalker"]
  }
]

Each record in this dataset has a url to redirect the queries, and a list of query_terms, that trigger the redirect.

For example, the query “star wars” should redirect to https://www.google.com/#q=star+wars.

You can download the dataset on GitHub. To learn how you can import this dataset in Algolia, see Importing from the dashboard

Configure the redirect index

To trigger the redirect only if the query exactly matches one of the query terms, add query_terms to the list of attributesForFaceting, so that you can filter on it:

1
2
3
4
5
$index->setSettings([
  'attributesForFaceting' => [
    'query_terms'
  ]
]);

Create a custom search client

You need to create a custom search client that simultaneously sends requests to the regular and redirect indices.

The request to the redirect index is sent with:

  • An empty query, so it doesn’t filter with it
  • A filter on query_terms with the content of the current query, to match only records that contain the current query exactly
  • hitsPerPage=1, as you only need the first record

Whenever a record matches in the redirect index, a redirect is triggered:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const algoliaClient = algoliasearch(
  'YourApplicationID',
  'YourSearchOnlyAPIKey'
);

const customSearchClient = {
  ...algoliaClient,
  async search(requests) {
    if (requests[0].params.query) {
      const { results } = await algoliaClient.search([
        ...requests,
        {
          indexName: 'your_redirect_index_name',
          params: {
            hitsPerPage: 1,
            facetFilters: [`query_terms:${requests[0].params.query}`],
          },
        },
      ]);

      const redirect = results.pop();

      if (redirect.hits.length) {
        window.location.href = redirect.hits[0].url;
      }

      return { results };
    } else {
      return algoliaClient.search(requests);
    }
  },
};

const search = instantsearch({
  indexName: 'instant_search',
  searchClient: customSearchClient,
});
Did you find this page helpful?