The solutions contained within this article may affect Google search rankings since they contain Javascript redirects. For this reason, they may not be suitable for long-term or large-scale deployment.
If you've deleted articles from your help center, you may have noticed that users occasionally still attempt to access the URLs associated with these articles. No matter the source of such traffic, you can automatically redirect users visiting these URLs to more useful pages.
This article includes several solutions that, in most cases, should help you redirect traffic:
- Redirecting a set of deleted articles to new article equivalents
- Redirecting untranslated articles to an existing language
- Redirecting all deleted articles to one specific page
- Generalizing or specifying which articles (or community posts) redirect to one specific page
Accessing your help center's custom JavaScript file
To implement any such solution, you must first access your help center's JavaScript file:
- Sign in to Zendesk Support as an administrator. Click the Zendesk Products icon (
) in the top bar, then select Guide.
- In the top right corner of Zendesk Guide select Guide Admin.
- Click the Customize design icon (
) in the sidebar.
- Click the theme you want to update to open it.
- Click the options menu (3 horizontal dots), then select Edit Code.
- Open the script.js file.
- Example: Redirecting a set of deleted articles to new article equivalents
Here's an example in which a help center has a set of old, deleted pages, each of which corresponds to a new page to which we are redirecting.
Within the first few lines of the JS file, you will see a line reading:
$(document).ready(function() {
Insert the following 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];
}
}
You'll need to customize a few things about this script before you can save it. Let's take a look at those below.
The first part of the code you'll edit is here:
var oldIds = ["217352077", "216552968"];
var newIds = ["216553098", "216552958"];
This is a list of the old article IDs and new article IDs. You are going to have to add your own article IDs here. To find an article ID, just view the article in your browser (or the URL from your analytics platform). The URL will look similar to this:
https://[YOURSUBDOMIAN].zendesk.com/hc/en-us/articles/203664386-Help-Center-guide-for-agents-and-end-users
In this case, the article ID is "203664386".
To redirect properly with this solution, they must be at the same position within the array. So in this case, an article whose URL contains "217352077" will now redirect to "216553098". And an article containing "216552968" will now redirect to "216552958".
Be sure to keep these IDs wrapped in quotes, as seen above, and separated by a comma in each case. So if you added a new set of redirects, it would look like this:
var oldIds = ["217352077", "216552968", "216552902"];
var newIds = ["216553098", "216552958", "216552944"];
Second, you will have to edit the URL in this line:
window.location.href = 'https://YOURSUBDOMAIN. zendesk.com/hc/en-us/articles/ ' + newIds[i];
Make sure that you use your subdomain, or alternately, use your entire help center URL as normally appears in your help center if it is white labeled to your own URL. Do not remove the end of the line, where it reads "+ newIds[i];". This is how the loop appends your new article ID to your URL. The code will fail without it.
Example: Redirecting untranslated articles to an existing language
Here's an example in which you have help center content in multiple languages. Let's say all of your contents exist in English, but only some of them exist in French and German. When a user tries to access those articles in French or German, they'll get an error page. This code allows redirects them away from the error page back to the existing 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, there's only one bit of customization that needs to be done. If your default help center language isn't English, you can replace that language. In the code, you'll see "en-us" appear in two places:
var notDefaultLanguage = window.location.href.indexOf('/en-us/') == -1;
and
var newURL = window.location.href.replace(/(.*\/hc\/)([\w-]+)(\/.*)/, "$1en-us$3");
You'll replace "en-us" in these two lines with your default language code. Find your default language code in the URL for any of your main articles. For example, a URL for a French-language help center page will look like
https://[YOURSUBDOMAIN].zendesk.com/hc/fr/articles/214943538
with "fr" representing the country code.
Example: Redirecting all deleted articles to one specific page
Here's another example in which a help center redirects all deleted articles (note: not including community posts) to one specific article.
Within the first few lines of the JS file, you will see a line reading:
$(document).ready(function() {
In this case, insert the following code directly below the line. Important note, inserting above (as with the previous example) will not work for this example:
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';
}
You'll need to customize the URL in this script before you can save it. Make sure to replace it with the URL of the article you want your help center to redirect to.
This solution will only work if we make sure that a ".not-found" class exists in your error page. So let's add one.
On the grey bar where the "JS" link appears, click on the "Home Page" link. Find and select "Error page" in that dropdown. In the error page, you'll find an area starting with the code {{#is error 'not_found'}}
. It will look something like this:
{{#is error 'not_found'}}
<h2>{{t 'nonexistent_page'}}</h2>
<p>{{t 'mistyped_address_or_moved_page'}}</p>
{{/is}}
You will want to add a new class to the h2 element (or any element within the 'not_found' #is tags) so that it looks like this:
<h2 class="not-found">{{t 'nonexistent_page'}}</h2>
Now you have all the elements in place that you'll need for a generally deleted article redirect.
Example: Generalizing or specifying which articles (or community posts) redirect to one specific page
You can get more specific or general with this solution by customizing the if statement. For example, to redirect from any deleted article or community post, change the if statement to:
if ( $(".not-found").length > 0 )
Alternately, 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 presented, these solutions will first briefly show the error page, then redirect to the new page. This is because we are using JS to search for an element within the page and to do that, we must first wait for the page to load.
8 Comments
Say we wanted to redirect all articles of one brand to a new brand is that possible?
Thanks
Hello @...,
This article's information with some changes to fit your use case is the best documentation we have available for this ask. I'll reach out to our moderator team to see if they can offer son insight into making these changes. We'll also share this post in our weekly digest so others can share that advice on how to accomplish this.
Best regards.
Hi @..., thanks, would the above solution work across brands, it should from what I've seen but I wonder how SEO might be impacted. We are keen to retain relevance whilst redirecting to our new brand. Any help from the moderator team would be greatly appreciated.
Hi David,
Articles still have the same ID
If your articles still have the same ID, then you need 1 redirect line.
Articles with changed IDs
You will need a single redirect line for each article if your articles no longer have the same article ID.
Hi everyone i need to redirect from a deleted .pdf
All the methods suggested dont seam to work, Can anyone help?
Hello,
I would like to redirect untranslated articles to the default language article.
For example, my default language is English and I have French translation. If the French translation does not exist for an article, the user who selects French, is currently redirected to the French home page. But I would like him to be redirected to the English article.
I tried the code bellow but it doesn't change anything...
The instructions on how to redirect for deleted articles, redirect all translations to English. How can I redirect from the deleted articles, to the new article in the language the user has? I tried removing the EN part from the URL, but strangely it made no difference.
Edit: To be super clear what code I'm talking about:
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]; } }
Edit 2: It turns out, removing the en/ *does* work for all languages, it just took a bizarrely long time after publishing to take effect. Whoohoo!
https://YOURSUBDOMAIN.zendesk.com/hc/articles/
Hi, we followed the instructions in the article above to redirect the untranslated article to the original article in English. However, it seems it's not working: this is an article in English that is not translated in German, but if you switch the language to German it redirects you to the home page instead of showing yout the article in English: https://zakeke.zendesk.com/hc/en-us/articles/4514828443932-Test-for-fallback
What's wrong?
Thanks
Carlo
Please sign in to leave a comment.