Question

I have deleted articles in my help center. Can I redirect traffic from my deleted help center articles?

Answer

Yes, regardless of the source of the traffic, you can automatically redirect users who visit these URLs to more valuable pages.

To implement these solutions:

  1. In Knowledge, select **Knowledge admin**
  2. Click the **Customize design** icon (Customize icon) in the sidebar
  3. Click **Customize** on your theme
  4. Click **Edit code**
  5. Click the **script.js** file

Edit_JavaScript_In_Theme_.gif

Note: The JavaScript redirects in the article may affect Google search rankings. They aren't suitable for long-term or large-scale deployment. Instead, if you have a developer, use the redirect rules API for page redirects on your help center.

Check the sections below for examples to implement common redirect workflows:

  • Example 1. Redirect a set of deleted articles to new article equivalents
  • Example 2. Redirect untranslated articles to a language that exists
  • Example 3. Redirect all deleted articles to one specific page
  • Example 4. Generalize or specify which articles or community posts redirect to a specific page

Example 1. Redirect a set of deleted articles to new article equivalents

In this example, the help center has a set of old, deleted pages where each corresponds to a new page to which you redirect. In the first few lines of the JavaScript file, you'll see a line that says:

$(document).ready(function() {
Note: This line might be absent if you use a recent version of the Copenhagen theme. In this case, place the code outside these listeners:
window.addEventListener("DOMContentLoaded", () => {
}

Insert this code directly above that line:

  var oldIds = ["217352077", "216552968"];
  var newIds = ["216553098", "216552958"];

  for (var i = 0; i < oldIds.length; i++){
    if (window.location.href.indexOf(oldIds[i]) > -1) {
      window.location.href = 'https://YOURSUBDOMAIN.zendesk.com/hc/en-us/articles/' + newIds[i]; 
    }
  }

Before you save it, edit the part of the code that lists the old article IDs and new article IDs:

  var oldIds = ["217352077", "216552968"];
  var newIds = ["216553098", "216552958"];

Add your article IDs instead of those in the example. To find an article ID, view the article in your browser. The URL looks similar to this example:

https://[YOURSUBDOMIAN].zendesk.com/hc/en-us/articles/203664386-Help-Center-guide-for-agents-and-end-users

In the URL above, the article ID is 203664386.

To redirect correctly, place old and new article IDs at the same position in the array. In this case, an article whose URL contains 217352077 now redirects to 216553098. The article that contains 216552968 now redirects to 216552958.

Keep these IDs in quotes and separated by a comma in each case. It looks like this example:

  var oldIds = ["217352077", "216552968", "216552902"];
  var newIds = ["216553098", "216552958", "216552944"];

Next, edit the URL in this line:

window.location.href = 'https://yoursubdomain.zendesk.com/hc/en-us/articles/' + newIds[i]; 

Use your Zendesk subdomain or your entire help center URL if it's white labeled. Don't remove the end of the line: "+ newIds[i];". The loop appends your new article ID to your URL. The code fails without it.

Example 2. Redirect untranslated articles to a language that exists

In this example, help center content exists in multiple languages. If all your content exists in English, but only some exists in French and German, users who access those articles in French or German get an error page. This code redirects visitors from the error page back to the English-language article.

var notDefaultLanguage = window.location.href.indexOf('/en-us/') == -1;
var isArticle = window.location.href.indexOf('/articles/') > -1;
var isErrorPage = $(".error-page").length > 0;


if ( isArticle && notDefaultLanguage && isErrorPage ) {
    var newURL = window.location.href.replace(/(.*\/hc\/)([\w-]+)(\/.*)/, "$1en-us$3");
    window.location.href =  newURL;
}  

In this example, you must do a small amount of customization. If your default help center language isn't English, replace that language. In the code, en-us appears in two places:

var notDefaultLanguage = window.location.href.indexOf('/en-us/') == -1;

And:

var newURL = window.location.href.replace(/(.*\/hc\/)([\w-]+)(\/.*)/, "$1en-us$3");

Replace en-us in these two lines with your default language code. Find your default language code in the URL for your main articles. For example, a URL for a French-language help center page looks like the example below:

https://[YOURSUBDOMAIN].zendesk.com/hc/fr/articles/214943538

Example 3. Redirect all deleted articles to one specific page

In this example, a help center redirects all deleted articles, but not community posts, to a specific article.

In the first few lines of the JavaScript file, you'll see a line that says:

$(document).ready(function() {
Note: This line might be absent if you use a recent version of the Copenhagen theme. In this case, wrap the suggested code into a new listener instead:
window.addEventListener("DOMContentLoaded", () => {
}

In this case, insert the following code directly below the line. Note, if you insert the above, the code won't work:

if ( window.location.href.indexOf('articles') > -1 && $(".not-found").length > 0 ) {
 window.location.href = 'https://[YOURSUBDOMAIN].zendesk.com/hc/en-us/articles/216553068-error-redirect';
}

In this script, replace the URL with the article URL for the redirect.

This solution only works if you ensure a .not-found class exists on your error page.

On the grey bar where the **JS** link appears, click the **Home Page** link. Find and select **Error page** in that dropdown. In the error page, you'll find an area that starts with the code {{#is error 'not_found'}} . It looks like this example:

{{#is error 'not_found'}}
 <h2>{{t 'nonexistent_page'}}</h2>
 <p>{{t 'mistyped_address_or_moved_page'}}</p>
 {{/is}}

Add a new class to the h2 element or any element within the 'not_found' #is tags so that it looks like this example:

<h2 class="not-found">{{t 'nonexistent_page'}}</h2>

Example 4. Generalize or specify which articles or community posts redirect to a specific page

Get more specific or general with this solution when you customize the if statement. For example, to redirect from any deleted article or community post, change the if statement to:

if ( $(".not-found").length > 0 )

Alternatively, to only redirect deleted articles that have the word buttermilk in the title, change the if statement to:

if ( window.location.href.indexOf('buttermilk') > -1 && $(".not-found").length > 0 )

Unlike the first solution, these solutions briefly show the error page and then redirect to the new page. The script uses JavaScript to search for an element in the page that must load first.

Disclaimer: This article is provided for instructional purposes only. Zendesk does not support or guarantee the code, which might become obsolete over time. Zendesk can't provide support for third-party technologies such as JavaScript, jQuery, or CSS. Post any issues you have in the comments section or try searching for a solution online.
Powered by Zendesk