How to get base/domain url in Visualforce page in Salesforce?

How to get base/domain url in Visualforce page in Salesforce?

{!$Site.BaseUrl} is used to get base url in Visualforce.

Sample Code:

Visualforce Page:

<apex:page 
    controller="SampleVisualforcePageController" 
    sidebar="false">
    <apex:repeat 
        value="{!listAccounts}" 
        var="acc">
        <a 
            href="{!$Site.BaseUrl}/{!acc.Id}" 
            target="_blank">
            {!acc.Name}
        </a>
        <br/>
    </apex:repeat>
</apex:page>

Apex Controller:

public class SampleVisualforcePageController {
    
    public List < Account > listAccounts {get;set;}
    
    public SampleVisualforcePageController() {
        
        listAccounts = [ 
            SELECT Id, Name 
            FROM Account 
            LIMIT 10
        ];
        
    }
    
}

Output:

Leave a Reply