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
In order 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. 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. In order 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".
In order 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:
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 it exists 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 that 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. 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 general deleted article redirect.
Example: Generalizing or specifiying 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 breifly show the error page, then redirect to the new page. This is due to the fact that we are using JS to search for an element within the page, and in order to do that, we must first wait for the page to load.
62 Comments
Is there a non-Javascript method to control redirected traffic? Javascript assumes that users have JS enabled. This also doesn't seem very scalable to manage content long-term, because we have limited ability to see the redirects, for control and for analytics.
There is an error in the following line:
Should be notDefaultLanguage.
Thanks for pointing on that error! It works for me now.
Thanks for the heads-up, Scott! I've contacted our Docs team so we can get that fixed.
I put in some redirects and it doesn't work on a phone - the "?mobile_site=true" parameter seems to cause a problem, although I'm not sure why. I've tested it other ways and it works fine, even with dummy content after the ID or "?asdfads=asdfasdfasd" and it redirects. But for some reason with "?mobile_site=true" it doesn't redirect.
I would like to second Stephen's point above - a non-JS method would be greatly appreciated!
Hey Brooke -
Thanks for the feedback, and welcome to the Zendesk Community!
Hey Steve! Welcome to the Community!
So I don't really know much of anything about JS, but I had a thought. Are you using the Copenhagen theme? It's a responsive theme so there's no mobile version per se...do you think that could be the issue?
Hey, wondering for an update on this! It's 2018, and we're hitting a lot of roadblocks where deleted links are becoming more costly for our product.
Would love to see a solution to set up redirect links for deleted articles - that doesn't involve Javascript.
+1
When deleting or unpublishing an article, it would be awesome if you could specify right then & there the new article that you want to redirect the old link to.
Hey all -
Our Product Managers are finding it too difficult to follow all of the feedback coming in on article comments, and have asked that you head on over to the product feedback topic in the Community to share feedback and get updates on what's coming.
Really?!
Give us normal 30x redirect facilities for articles. How hard can that be? We're using two Help Centers, and I have to maintain two separate JS redirect banks on my HCs. Also, your JS code here is pretty bad. It doesn't allow for easy maintenance of the records you're updating, without refactoring it, because you have to try to match the redirects in the list in a 1:1 fashion. I had a developer at my company see what you had offered, and how upsetting maintaining just a dozen redirects turned into (bad, bad, bad jumble). In fact, here's the refactored code:
Provide a real facility for doing this. Redirects are a huge part of how Zendesk works, and the fact that it's not included in the feature suite, and that you've asked us to abandon this thread for the feedback forums, where there is no thread on this topic (I searched 25 pages before I couldn't take it anymore) -- I am utterly stunned that you're claiming this is too difficult to follow.
Perhaps you should put a product architect, instead of a product manager, on this problem. The fact that the article was published over 2 years ago, with the JS code to work around the redirect problem we have with this system, is inexcusable. It just needs to be fixed. An explanation is not necessary. A solution is, however. Don't even get me started on how hard it is to pull a list of all tags out for reporting. Some of the basic functions that ZD excludes makes for some serious sore spots, and that they've been unaddressed through a re-brand (over 14 months ago, btw), and are now being pushed around communities, baffles me.
I also noticed that you've deleted most of the comments in this thread, several of them belonging to me. This problem has been around FOREVER, and was addressed with this article beginning in 2014. I've saved this answer so I can repost it, should your delete finger get "itchy".
"Product Managers are finding it too difficult to follow all of the feedback coming in on article comments." So you expect us, your customer, to sift through 121 pages of feedback points to submit our own comments? How about making it easy for us to be customers and to give feedback, and Zendesk taking the responsibility for cultivating our comments in a way that works for everyone?
@stephen @joe: If it helps, I found a page in the Community that fits the topic and just copied + pasted my comment from above there.
For what it's worth, I'm in complete agreement with both of your comments too.
Hey Stephen -
We're trying to simplify it by keeping all of the conversations in the Product Feedback threads. You can do a quick search to see if there's a thread relevant to your feedback and if not, create a new post.
We're trying very hard to cultivate your comments in a way that works for everyone, and that may be reflected in new processes or a different means of collecting feedback altogether in the future. For the moment, we are trying to focus things in the Product Feedback topic.
Hi,
How do I redirect a knowledgebase article to an external URL?
In this case, the URLs are on our company website.
Hi David, in case you've missed it, here is my comment about this question:
https://support.zendesk.com/hc/en-us/community/posts/360004154368-How-do-I-redirect-a-knowledgebase-article-to-an-external-URL-?page=1#community_comment_360001898947
Let me know if this doesn't help. :)
Awesome! Thank you, Vladan :)
Hey @vladen, @nicole / product managers at Zendesk. It's pretty disappointing that you don't offer basic 301 redirect functionality and we have to resort to JS hacks to make this work.
Issues like this make it seem like our business is not something you're interested in keeping in the long run.
Is there a way to do this with deleted sections (instead of articles) - we've shifted articles from a discontinued section.
Hey Kelly, sorry for the delayed reply! Somehow I've missed your comment.
So, if you mean to redirect an old Section page to another, yes you can do that.
This code is still valid for such a case, but instead of article ID, you need to put Section ID.
Please try it out and let us know how it goes. Thanks!
Hi Vladan,
Not a problem! I gave it a try today but I had to create a separate section in my JS file and update the link so it was using the /categories/ URL instead of /articles. It's worked, which is great.
This is the code I used (I've stripped out our unique information of course):
var oldIds = ["123"];
var newIds = ["456"];
for (var i = 0; i < oldIds.length; i++){
if (window.location.href.indexOf(oldIds[i]) > -1) {
window.location.href = 'https://[company].zendesk.com/hc/en-us/categories/' + newIds[i];
}
}
Hi Kelly! Glad to hear you got it working!
Hey Kelly, glad you've sorted it out!
Also, thanks for sharing your code, it looks great!
Hi all!
Like everyone else, I really need redirection functionality. While I prefer a 30x redirect, this isn't currently possible so, similar to the folks above, I have built a javascript workaround that I would like to share.
https://github.com/scotthavard92/Zendesk-Help-Center-Redirection
The instructions are in the repository, but it largely involves the same procedure as the solutions above. Throw the snippet into your <head>, which is [in most cases] located in the document_head.hbs file.
Ideally this is a little more scaleable than looping through every redirection option because I used a hash map instead. So based on an old article ID it will look up a new article ID, and not loop through every option.
The list is also easier to maintain, as it maps out cleanly:
old id 1 = new id 1, old id 2 = new id 2, etc.
Feel free to reach out with any questions!
I implemented the redirecting untranslated articles to an existing language example. It works well except if the other language's version exists as a draft. Does anyone have suggestions on how to edit the code such that it also works for draft/unpublished versions? This is mostly used when we're staging a language to turn on at once and would like them directed to the default until it's live.
I think this might have to do with how the drafts send people to log in. I'd prefer it would direct them to the default language rather than log-in process.
Hey Monica,
Any chance you could provide the existing code you're implementing? This may help other users jump in and offer up some alternatives for you.
Thanks!
I'm using the code from the redirecting untranslated articles to an existing language example in the script.js file.
// Redirect Untranslated to Default Language
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;
}
Since the language variant is a draft, it's routing to the log-in function which we have a JSON Web Token (JWT) set up. I am not sure where in the code the system directs to log in rather than the error page for a draft language variant. Ideally, I'd want to interrupt the redirect to log-in and send them to the default page.
Hi Monica,
As far as I know, when an anonymous user attempts to access an article they do not have access to, they will automatically be directed to the login page.
I'm not exactly sure how you would go about redirecting them to a page they do have access to other than using custom code.
I'll leave this post open in case other users want to jump in and offer up advice on how to set this up on your end.
I'll also reach out to our Zendesk Moderators to see if they have any guidance.
Cheers!
Hi all - I'm trying to use the script for "Redirecting untranslated articles to an existing language", but it doesn't seem to work. I still get the error page instead of being redirected. Has anyone had success?
Is it because the code has error-page but the template is error_page.hbs? I swapped the hyphen with an underscore and it doesn't make a difference.
Please sign in to leave a comment.