we have a functionality where users get an email with a link to a Salesforce site (that hosts a VF Page). Once they click on the link in the email it takes that that that site. But I am struggling to generate a string that will dynamically generate URL in my class.

I am working in a scratch org and created a Domain and Site; my final URL looks like this for the site: https://sandbox-xyzwys.cs68.force.com

But Prod already has domain activated and other sites created (so does full sandbox). So when I migrate work to full sandbox and Prod my site migrates over, but its URL changes: https://dev-ABCDZ.cs70.force.com

If I query Domain SELECT Domain,DomainType,Id FROM Domain it returns nothing in my scratch org and does return completely different Domain in the sandbox: https://dev-QAZXSW.cs70.force.com

Please help me generate a string that will dynamically crate a valid URL that can be referenced in APEX and can be easily migrated between the orgs.