- 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 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 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.Using the Visual Editor
- In the Algolia dashboard, select your application and index, open Rules, and select New rule > Visual Editor.
- Click Set query condition(s), select Contains, enter
help, and click Apply. - In Consequences, expand More consequences and select Redirect. Enter the URL for your support page and click Apply.
- Click Review and Publish, review the rule, and click Publish.
Using the API
To run the code examples on this page, install the latest API client. To add a rule with the API, use thesaveRule 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:
JSON
Handle redirects in your user interface
After adding the rule, you can trigger the redirect in your UI when therenderingContent.redirect property is present in the API response. You have two options:
- Using the redirect plugin for the Autocomplete UI library
- Implement your own logic
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 either 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 contains “help”.Using the dashboard
You can add this rule from the Algolia dashboard using either the Visual Editor or the Manual Editor.- Visual Editor
- Manual Editor
- Open Rules and select New rule > Visual Editor.
- Click Set query condition(s), select Contains, enter
help, and click Apply. - In Consequences, expand More consequences and select Return custom data.
Enter
{ "redirect": "https://www.algolia.com/support" }in Value and click Apply. - Click Review and Publish, review the rule, and click Publish.
Using the API
To add a rule with the API, use thesaveRule method.
When setting a rule, you need to define a condition and a consequence.
Handle redirects in your user interface
After adding the rule, you can trigger the redirect in your UI when theuserData 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 users 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 a .Dataset
JSON
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.
- Download the dataset
- To learn how to import the dataset, see Import from the dashboard
Configure the redirect index
To trigger the redirect only if the query exactly matches one of the query terms, addquery_terms to the list of attributesForFaceting,
so that you can filter on it:
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_termswith the content of the current query, to match only that contain the current query exactly hitsPerPage=1, as you only need the first record