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
Praveen_K.ax1208Praveen_K.ax1208 

how to REWRITE the Salesforce urls to user friendly urls

Hi All,

 

I have a requirement to REWRITE the Salesforce urls to user friendly urls..

Can anyone post the working example on this..

 

This will be more helpfull for doing my task..

 

 

Thanks in Advance..

Praveen K. 

Devendra@SFDCDevendra@SFDC

 

 

Hi Praveen,

 

Can you please specify which Salesforce URL you are referring to?

 

Thanks,

Devendra

Praveen_K.ax1208Praveen_K.ax1208

Hi Devendra,

 

Thanks for your immediate reply,

 

https://c.ap1.visual.force.com/apex/mycontact?id=00390000009IBJw

 

Above is the url i want to rewrite.

 

Mycontact is the VF page i have created 

 

global class myURLRewriter 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 = '/apex/mycontact?id=';
 // The first global method for mapping external URL to an internal one
 global PageReference mapRequestUrl(PageReference myFriendlyUrl){
    String url = myFriendlyUrl.getUrl();
    System.debug('******************************'+url);
    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
       Contact site_page = [select id from Contact 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
        Contact site_page2 = [select name from Contact 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;
  }
}

 

 

If you have any other apex classes for rewiting urls please post those also..

 

 

Also provide how to test this...

 

 

Thanks,

Praveen K.

 

 

Devendra@SFDCDevendra@SFDC

 

Hi Praveen,

 

This is the same you are looking for.

 

Hope this helps.

 

Thanks,

Devendra

 

 

Praveen_K.ax1208Praveen_K.ax1208

it may work, But i want to know how to test the rewrited url..

 

With test class i can able to see the rewrited urls in debug logs... but i have to see the same in address bar..

 

Can u provide any suggestions on this ??

 

Thanks,

Praveen K.

Devendra@SFDCDevendra@SFDC

 

No, i dont have much idea on this.:(

 

Thanks,

Devendra