If you’re open to running User Scripts, this one called Fediverse Redirector redirects all Community, Post, and User links to your home instance. It works well.
-
Just click install on that page - it will be added to TamperMonkey (or similar).
-
Then click settings (under the script) and enter your home lemmy instance.
And to directly answer your question: the raw code is visible in that repo, so you could explore how the post
redirect query was constructed.
This monkey-script is awesome: Lemmy Universal Link Switcher
It converts all links to your home instance (in your browser).