How to redirect a specific topic to a category?

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!

1 Like

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>
1 Like

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.

2 Likes

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.