URL Rewriting For Force.com Site

In Salesforce, the Site provides a built-in logic that helps you to display user friendly URL and links instead of complex URL and links.URL rewriting allows search engines to better index your site pages. Using friendly URLs also allows you to replace the page name and id

In Salesforce, the Site provides a built-in logic that helps you to display user friendly URL and links instead of complex URL and links.URL rewriting allows search engines to better index your site pages. Using friendly URLs also allows you to replace the page name and id. For example, the URL http://developerkanagu.cs11.force.com/homepage/mycontact?id=003Z000000bm5im can be rewritten with a new URL http://developer-kanagu.cs11.force.com/homepage/mycontact/Nikhil%20Abraham that is easier to read.

To rewrite URLs for site, create an Apex class that maps the original URL to a user friendly URL and add that Apex class to your site.

Creating the Apex Class

global class yourClass implements Site.UrlRewriter
{
global pageReference mapRequestUrl (PageReference your FriendlyUrl)
global pageReference [ ]  generateUrlFor (pageReference[ ] yoursalesforceUrls);
}

To learn more about Site.urlRewriter interface, see Salesforce UrlRewriter Interface

When creating the apex class, you should follow the following guidelines:

1. Class and methods should be a global

2. You should use both method mapRequestUrl() and GenerateUrlFor() . If you don’t need one of the methods, simply specify “return null”.

3. URL rewriting works only with visualforce page. URL rewriting does not work with standard salesforce page, image and other entities.

4. Use !URLFOR function to rewrite your site url page link.

URL rewriting with Site

1. From setup, click Develop | Sites

2. Click New.

After the New button is clicked, the below screen will open. Then add the domain path and URL rewriter class. The following screen shots shows these fields.

url2209-1.png

3. Click Save.

Site Page

<apex:page standardController="contact">
    <apex:pageBlock title="Contact informations.....">
        <apex:outputLink value="{!URLFOR($Page.mycontact,null,  [id=id])}">
                </apex:outputLink>
    </apex:pageBlock>
    <apex:detail relatedList="false"/>
</apex:page>

Apex Class

global with sharing class myRewriter implements Site.UrlRewriter {
// Variable to represent the friendly URLs for Page
   String contact_information = '/mycontact/';
// Variable to represent the custom Visualforce page that displays the page information
   String contact_information_vf_page = '/mycontact?id=';
//The first global method for mapping external URL to an internal one
    global PageReference mapRequestUrl(PageReference myFriendlyUrl){
        String url = myFriendlyUrl.getUrl();

        if(url.startsWith(contact_information)){
                        String name = url.substring(contact_information.length(), url.length());

            //Select id of the page that maps the name from the URL
            Contact con = [SELECT Id FROM Contact WHERE Name in : name LIMIT 1];
          //construct a new page reference in the form of mycontact visualforce page
            return new PageReference(contact_information_vf_page + con.id);
        }
        return null;
    }
    //The second global method maps the internal ids to URLs
    global List<PageReference> generateUrlFor(List<PageReference> mySalesforceUrls)
        {
  //List of pages to return after all links have been evaluated.
       List<PageReference> myFriendlyUrls = new List<PageReference>();
       List<id> accIds = new List<id>();
      for(PageReference mySalesforceUrl : mySalesforceUrls){
     //get the URL of the page
       String url = mySalesforceUrl.getUrl();
    //if this look like a page that need to be mapped, transform it
      if(url.startsWith(contact_information_vf_page)){
//Extract id from the query parameter       
String id= url.substring(contact_information_vf_page.length(),
                        url.length());
                        accIds.add(id);
            }
        }
//Query the name of the contact to put into URL
    List <contact> contacts= [SELECT Name FROM contact WHERE Id IN :accIds];
    Integer counter = 0;
    String a = ((contacts.get(counter).name)+'custom');
     for(PageReference mySalesforceUrl : mySalesforceUrls) {
   // Get the URL of the page.
      String url = mySalesforceUrl.getUrl();
    if(url.startsWith(contact_information_vf_page)){
         myFriendlyUrls.add(new PageReference(contact_information + a ));
         counter++;
       } else {
      myFriendlyUrls.add(mySalesforceUrl);
       }
    }
    return myFriendlyUrls;
  
  }
}

After completing the above steps, click the Public Access Settings in the Site details page.
Edit the URL Rewriter profile, add the Visualforce page in Enable Visualforce page access section, and add the Apex class into Enable Apex Class access section.
Now, grant Read permission to the object of the records you want to see. In OWD settings, grant Public Read Only access for the object.

Following are the screenshots of URLs without and with URL Rewriting

url2209-2.png
url2209-3.png

About MST

At MST Solutions our cornerstone is to adapt, engage and create solutions which guarantee the success of our clients. The talent of our team and experiences in varied business verticals gives us an advantage over other competitors.

Recent Articles

Work with us.

Our people aren’t just employees, they are key to the success of our business. We recognize the strengths of each individual and allow them time and resources to further develop those skills, crafting a culture of leaders who are passionate about where they are going within our organization.