Skip to content

Instantly share code, notes, and snippets.

@fractastical
Created May 24, 2011 21:38
Show Gist options
  • Save fractastical/989791 to your computer and use it in GitHub Desktop.
Save fractastical/989791 to your computer and use it in GitHub Desktop.
@umityalcinalp URLRewriter
http://blog.sforce.com/sforce/2010/05/url-rewriting-for-your-customizing-your-site-urls.html
global class ApexClassName implements Site.UrlRewriter {
global PageReference mapRequestUrl(PageReference externalUrl) {//}
global List<PageReference> generateUrlFor(List<PageReference> myForcedotcomUrls) {//...}
}
Note that a developer may not need all of the methods and may choose to return a null.
Here is the implementation of our the global Apex class that maps the URLs from our example:
global class myRewriter implements Site.UrlRewriter {
//Variables to represent the friendly URLs for pages
String DIRECTORY = '/friendly/';
//Variables to represent my custom Visualforce pages that display page information
String VISUALFORCE_PAGE = '/page?pageid=';
// The first global method for mapping external URL to an internal one
global PageReference mapRequestUrl(PageReference myFriendlyUrl){
String url = myFriendlyUrl.getUrl();
if(url.startsWith(DIRECTORY)){
String name = url.substring(DIRECTORY.length(),url.length());
//Select the ID of the page that matches the name from the URL
Page__c site_page = [select id from Page__c where name =:name LIMIT 1];
//Construct a new page reference in the form of my Visualforce page
return new PageReference(VISUALFORCE_PAGE + site_page.id);
}
//If the URL isn't in the form of a cmasforce page, continue with the request
return null;
}
// The second global method for mapping internal Ids to URLs
global List<PageReference> generateUrlFor(List<PageReference> mySalesforceUrls){
//A list of pages to return after all the links have been evaluated
List<PageReference> myFriendlyUrls = new
List<PageReference>();
for(PageReference mySalesforceUrl : mySalesforceUrls){
//Get the URL of the page
String url = mySalesforceUrl.getUrl();
//If this looks like a page that needs to be mapped, transform it
if(url.startsWith(VISUALFORCE_PAGE)){
//Extract the ID from the query parameter
String id= url.substring(VISUALFORCE_PAGE.length(), url.length());
//Query for the name of the cmsforce page to put in the URL
Page__c site_page2 = [select name from Page__c where id =:id LIMIT 1];
//Construct the new URL
myFriendlyUrls.add(new PageReference(DIRECTORY+ site_page2.name));
}
else {
//If this doesn't start like an cmsforce page, don't do any transformations
myFriendlyUrls.add(mySalesforceUrl);
}
}
//Return the full list of pages
return myFriendlyUrls;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment