September 2, 2011

$Page — Getting the URL of a Sites Page

Nothing revolutionary here, just something I needed to find quickly and didn't appear to be obviously apparent, of course it's in the documentation etc., I just figured I'd share it too so that the answer is even easier to find.

Scenario##

You're exposing some Visualforce pages over sites, and in one of those pages you want to link to another.

Solution##

My first reaction was to use ApexPages.currentPage() in the controller to grab the current URL which I could then modify, but that's not the correct (or simple) approach! All you need is to utilise one of  the Visualforce Global Variables: $Page.

{!$Page.PageName} 
<!-- For Example --> 
<a href="{!$Page.Page2}">Link to Page 2</a>

Too easy!