I've been working on a website for a couple of months and it started to get some viewership after making improvements to SEO, while it was still accessible only through a .heroku.com domain. I thought to myself that it was time to purchase a .com domain on namecheap, since that will make the site seem more legit and might lead to more clicks.
I finished the setup and my site can both be found at example.com and example.herokuapp.com. At first all of the search results in google still lead to the .herokuapp.com site and I was on the first page for many queries that I care about. But now my page is nowhere to be found. I believe this might be because google sees this as duplicate content and punishes me harshly for it.
I basically want all of the links in google to lead to the regular .com site and not have the herokuapp.com site accessible anymore or completely hide it from all search engines. I stumbled upon a few possible solutions, but none of them seem very clean or practical since my site already consists of over 400 pages. (How to get rid of Heroku App domain in the search resutls)
What would be the best way to go about this ?
if (hostname=='example.heroku.com') res.redirect(301, "xxx");
That may not be available, which is why the canonical links are recommended in the answers to How to get rid of Heroku App domain in the search resutls