function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

How to get the site url from Apex Code

Hi Guys,

Is thera any way I can got the site url from Apex Code?

I tried Site.getPrefix() , Site.getDomain() and getCurrentSiteUrl. but all of them return null.

Do I need setup some information after the site is ready.

Thanks in advance.


Please give a try using URL methods. More details for all URL methods can be found here-> 




Tejpal KumawatTejpal Kumawat

Hi try to this it will be definately works...


public String getSiteUrl(){
return Url.getSalesforceBaseUrl().toExternalForm();


Thanks guys,


from Url.getSalesforceBaseUrl().toExternalForm(), the result like which is salesforce internal domain.


Is it possible to get the site domain name something like ?




What I am doing is to make the attachments public via site,  the url pattern for attachment is {!$Site.Prefix}/servlet/servlet.FileDownload?file={!List.Image},


Unfortunately, I have to expose those the attachment image via web service. so I have to get the site domain from Apex Code.


Or Is there any way to expose images directly?




Hey tony, if you use Site.getCurrentSiteUrl it will be provide the url of site...If it is not provide then change the public settings in the site settings... then also check


Thanks Dhurav,


I still got null using  Site.getCurrentSiteUrl.


I couldn't find any settings for this url from public settings.


Can you point our what inforamtion I need to setup in public settings ?


Thanks a lot