How to create custom redirects via JavaScript?
In this guide, we will cover how to create custom redirects directly from the customize section of your Knowledge Base.
Custom redirects are redirects that are not covered on the Article Redirects feature, learn more at How to Create Article/URL Redirects. For instance:
- Redirects from valid URLs on your KB;
- Redirects based on search parameters or event listeners;
In order to redirect users before the page is fully loaded, place the code in the Main Layout file, inside the HEAD tag.
Multilingual KBs
If your Knowledge Base is multilingual, please notice that you have multiple Main Layout files, each one specific to a language section.
Example
<script>
if (location.pathname === "/en_US") {
location.href = "/en_GB";
}
</script>
In this example, we created a redirect from the English (US) language section to the English (UK) section. You might want to use liquid tags to create more robust/complex redirects, learn more at Shopify/Liquid Variables and Objects Reference.