Get the Salesforce Internal (non-Site) URL from a Site

(This is the opposite question of Get Site URL from apex.)

I’m running Apex in a request on a Site page. URL.getSalesforceBaseUrl().toExternalForm() gives me https://siteprefix.force.com

I want my actual instance URL: https://mydomain.my.salesforce.com or even https://pod.salesforce.com. How can I get it?

Answer

I recently faced this exact scenario and also found an answer to this. Use this code snippet to get salesforce instance url when working on a site page

ApexPages.currentPage().getHeaders().get('X-Salesforce-Forwarded-To');

Per the documentation:

X-Salesforce-Forwarded-To : The fully qualified domain name of the Salesforce instance that is handling this request. This header is always set on HTTP and HTTPS requests that are initiated outside of Salesforce’s data centers.

Related PageReference headers documentation

Attribution
Source : Link , Question Author : Benj , Answer Author : Mark Pond

Leave a Comment