Bulk importing Redirects to New Discourse Topics

Hello everyone,

I recently managed to migrate my site from an old custom platform to Discourse successfully. However, I’m now facing the challenge of importing permalinks/redirects to the new Discourse topics. I have the IDs of all posts created in Discourse and I’m exploring options to import these redirects via API or command lines, as manually importing them due to the large amount is not feasible.

Could anyone please advise if there are any methods or tools available to efficiently import redirect links to the new Discourse topics? Your insights and suggestions would be greatly appreciated.

Thank you in advance!

If your migration was based on an import script then topics (and probably posts) include an import_id custom field. You can then use those custom fields, Permalinks, and SiteSetting.permalink_normalizations to make redirects.

You can test in rails with something like

tcfs=TopicCustomField.where(name: 'import_id')
pcfs=PostCustomField.where(name: 'import_id')

If you don’t have those, then it’s going to be Really Hard.

If you do have those, then you can do something like

tcfs.each do |tcf|
  Permalink.create(url: "imported-topic/#{tcf.value}", topic_id: tcf.topic_id)
end

And then make a PermalinkNormalization that redirects the path for topics to /imported-topic/IMPORTED_ID.

You can grep the import_scripts` to find some other import scripts that set Permalinks and the permalink normalization.

1 Like

Thank you, Jay, for your response. Instead of using the import script for migration, I opted for API integration due to the custom structure. I developed a Python script to import posts using the API, and I have successfully fetched new topic IDs for each topic. Despite importing old URLs with new topic IDs/URLs across approximately 6k pages, none of the mappings are functioning as expected. Could you please advise on what I might be missing and how to rectify this issue? Your guidance would be greatly appreciated.