How to redirect a specific topic to a category?

AI-generated summary

The discussion revolves around redirecting a specific topic to a category route in Discourse. bartv initially asked for help with this issue, providing an example of the desired redirect (/t/slug/123 => /c/slug/456).

bartv then shared a custom JavaScript solution using a Discourse plugin, which appears to work for their use case. However, JammyDodger suggested using the “external or relative url” option in Permalinks as an alternative solution.

bartv clarified that the topics are not deleted, but rather need to be pinned as a navigation tool, and tested that Permalinks do not work for existing URLs. JammyDodger confirmed that Permalinks only work for deleted topics, and not for existing URLs.

I’m a bit stuck on this one: I’d like to redirect from a specific topic to a category route, something like this:

/t/slug/123 => /c/slug/456

I realise the permalinks in the admin aren’t meant for existing Discourse URLs, and that this could be done with some custom code. Does anyone have an example to point me in the right direction?

Thanks!

Ok, I’ve cobbled together a solution and it seems to work. Did I do this correctly, or am I doing terrible things? :see_no_evil:

<script type="text/discourse-plugin" version="0.8"> 
    // Modify the topic route. 
    api.modifyClass('route:topic', {
    
      beforeModel( transition ) {
        var params = this.paramsFor('topic');

        // English
        if( params['id'] == 123 ) {
          this.router.transitionTo('/c/tutorials/english/29'); // Implicitly aborts the on-going transition.
        }
        
        // French
        if( params['id'] == 456 ) {
          this.router.transitionTo('/c/tutorials/french/30'); // Implicitly aborts the on-going transition.
        }
                
      }
    
    });
</script>

Can you not do this with the permalinks?

I’ve had a quick test using the ‘external or relative url’ option (though there may be a better one), and it seemed to work okay?

Just make sure to test with a user who doesn’t have the ability to see deleted topics/posts.

My topics aren’t deleted though, I just need them pinned as a navigation tool. I tested this and Permalinks don’t work for existing URLs, do they?

Ah, no. Permalinks would only work if the topic is deleted, otherwise you won’t be redirected and will land on the successful url.