Newer Version Available
Rewrite URLs for Salesforce Sites
For example, let's say that you have a blog site. Without URL rewriting, a blog entry's URL might look like this: https://myblog.my.salesforce-sites.com/posts?id=003D000000Q0PcN
With URL rewriting, your users can access blog posts by date and title, say, instead of by record ID. The URL for one of your New Year's Eve posts might be: https://myblog.my.salesforce-sites.com/posts/2019/12/31/auld-lang-syne
You can also rewrite URLs for links shown within a site page. If your New Year's Eve post contained a link to your Valentine's Day post, the link URL might show: https://myblog.my.salesforce-sites.com/posts/2019/02/14/last-minute-roses
To rewrite URLs for a site, create an Apex class that maps the original URLs to user-friendly URLs, and then add the Apex class to your site.
To learn about the methods in the Site.UrlRewriter interface, see UrlRewriter Interface.
Creating the Apex Class
1global class yourClass implements Site.UrlRewriter {
2 global PageReference mapRequestUrl(PageReference
3 yourFriendlyUrl)
4 global PageReference[] generateUrlFor(PageReference[]
5 yourSalesforceUrls);
6}- Class and Methods Must Be Global
- The Apex class and methods must all be global.
- Class Must Include Both Methods
- The Apex class must implement both the mapRequestUrl and generateUrlFor methods. If you don't want to use one of the methods, simply have it return null.
- Rewriting Only Works for Visualforce Site Pages
- Incoming URL requests can only be mapped to Visualforce pages associated with your site. You can't map to standard pages, images, or other entities.
- To rewrite URLs for links on your site's pages, use the !URLFOR function with the $Page merge variable. For example,
the following links to a Visualforce page named
myPage:
1<apex:outputLink value="{!URLFOR($Page.myPage)}"></apex:outputLink> - See the “Functions” appendix of the Visualforce Developer's Guide.
- Encoded URLs
- The URLs you get from using the Site.urlRewriter interface are encoded. If you need to access the unencoded values of your URL, use the urlDecode method of the EncodingUtil Class.
- Restricted Characters
- User-friendly URLs must be distinct from Salesforce URLs. URLs with a 3-character entity prefix or a 15- or 18-character ID aren’t rewritten.
- You can’t use periods in your user-friendly or rewritten URLs, except for the .well-known path component, which can’t be used at the end of a URL.
- Restricted Strings
- You can’t use the following reserved strings as the first path component
after a site’s base URL in either a user-friendly URL or a rewritten
URL. Some examples of the first past component after a site’s base URL
are baseURL in
https://MyDomainName.my.salesforce-sites.com/baseURL,
https://MyDomainName.my.salesforce-sites.com/pathPrefix/baseURL,
https://custom-domain/pathPrefix/baseURL, and
https://MyDomainName.my.salesforce-sites.com/pathPrefix/baseURL/another/path.
- apexcomponent
- apexpages
- aura
- chatter
- chatteranswers
- chatterservice
- cometd
- ex
- faces
- flash
- flex
- home
- id
- ideas
- idp
- images
- img
- javascript
- js
- knowledge
- lightning
- login
- m
- mobile
- ncsphoto
- nui
- push
- resource
- saml
- sccommunities
- search
- secur
- services
- servlet
- setup
- sfc
- sfdc
- sfdc_ns
- sfsites
- site
- style
- vote
- widg
- You can't use the following reserved strings at the end of a rewritten
URL path:
- /aura
- /auraFW
- /auraResource
- /AuraJLoggingRPCService
- /AuraJLVRPCService
- /AuraJRPCService
- /dbcthumbnail
- /HelpAndTrainingDoor
- /htmldbcthumbnail
- /l
- /m
- /mobile
- Relative Paths Only
- The PageReference.getUrl() method only returns the part of the URL immediately following the host name or site prefix (if any). For example, if your URL is https://mycompany.my.salesforce-sites.com/sales/MyPage?id=12345, where “sales” is the site prefix, only /MyPage?id=12345 is returned.
- You can't rewrite the domain or site prefix.
- Unique Paths Only
- You can't map a URL to a directory that has the same name as your site prefix. For example, if your site URL is https://acme.my.salesforce-sites.com/help, where “help” is the site prefix, you can't point the URL to help/page. The resulting path, https://acme.my.salesforce-sites.com/help/help/page, would be returned instead as https://acme.my.salesforce-sites.com/help/page.
- Query in Bulk
- For better performance with page generation, perform tasks in bulk rather than one at a time for the generateUrlFor method.
- Enforce Field Uniqueness
- Make sure the fields you choose for rewriting URLs are unique. Using unique or indexed fields in SOQL for your queries may improve performance.
Adding URL Rewriting to a Site
- From Setup, enter Sites in the Quick Find box, then select Sites.
- Click New or click Edit for an existing site.
- On the Site Edit page, choose an Apex class for URL Rewriter Class.
- Click Save.
Code Example
In this example, we have a simple site consisting of two Visualforce pages: mycontact and myaccount. Be sure you have “Read” permission enabled for both before trying the sample. Each page uses the standard controller for its object type. The contact page includes a link to the parent account, plus contact details.
Before implementing rewriting, the address bar and link URLs showed the record ID (a random 15-digit string), illustrated in the “before” figure. Once rewriting was enabled, the address bar and links show more user-friendly rewritten URLs, illustrated in the “after” figure.
The Apex class used to rewrite the URLs for these pages is shown in Example URL Rewriting Apex Class, with detailed comments.
Example Site Pages
This section shows the Visualforce for the account and contact pages used in this example.
1<apex:page standardController="Account">
2 <apex:detail relatedList="false"/>
3</apex:page>1<apex:page standardController="contact">
2 <apex:pageBlock title="Parent Account">
3 <apex:outputLink value="{!URLFOR($Page.mycontact,null,
4 [id=contact.account.id])}">{!contact.account.name}
5 </apex:outputLink>
6 </apex:pageBlock>
7 <apex:detail relatedList="false"/>
8</apex:page>Example URL Rewriting Apex Class
1global with sharing class myRewriter implements Site.UrlRewriter {
2
3 //Variables to represent the user-friendly URLs for
4 //account and contact pages
5 String ACCOUNT_PAGE = '/myaccount/';
6 String CONTACT_PAGE = '/mycontact/';
7 //Variables to represent my custom Visualforce pages
8 //that display account and contact information
9 String ACCOUNT_VISUALFORCE_PAGE = '/myaccount?id=';
10 String CONTACT_VISUALFORCE_PAGE = '/mycontact?id=';
11
12 global PageReference mapRequestUrl(PageReference
13 myFriendlyUrl){
14 String url = myFriendlyUrl.getUrl();
15
16 if(url.startsWith(CONTACT_PAGE)){
17 //Extract the name of the contact from the URL
18 //For example: /mycontact/Ryan returns Ryan
19 String name = url.substring(CONTACT_PAGE.length(),
20 url.length());
21
22 //Select the ID of the contact that matches
23 //the name from the URL
24 Contact con = [SELECT Id FROM Contact WHERE Name =:
25 name LIMIT 1];
26
27 //Construct a new page reference in the form
28 //of my Visualforce page
29 return new PageReference(CONTACT_VISUALFORCE_PAGE + con.id);
30 }
31 if(url.startsWith(ACCOUNT_PAGE)){
32 //Extract the name of the account
33 String name = url.substring(ACCOUNT_PAGE.length(),
34 url.length());
35
36 //Query for the ID of an account with this name
37 Account acc = [SELECT Id FROM Account WHERE Name =:name LIMIT 1];
38
39 //Return a page in Visualforce format
40 return new PageReference(ACCOUNT_VISUALFORCE_PAGE + acc.id);
41 }
42 //If the URL isn't in the form of a contact or
43 //account page, continue with the request
44 return null;
45 }
46 global List<PageReference> generateUrlFor(List<PageReference>
47 mySalesforceUrls){
48 //A list of pages to return after all the links
49 //have been evaluated
50 List<PageReference> myFriendlyUrls = new List<PageReference>();
51
52 //a list of all the ids in the urls
53 List<id> accIds = new List<id>();
54
55 // loop through all the urls once, finding all the valid ids
56 for(PageReference mySalesforceUrl : mySalesforceUrls){
57 //Get the URL of the page
58 String url = mySalesforceUrl.getUrl();
59
60 //If this looks like an account page, transform it
61 if(url.startsWith(ACCOUNT_VISUALFORCE_PAGE)){
62 //Extract the ID from the query parameter
63 //and store in a list
64 //for querying later in bulk.
65 String id= url.substring(ACCOUNT_VISUALFORCE_PAGE.length(),
66 url.length());
67 accIds.add(id);
68 }
69 }
70
71 // Get all the account names in bulk
72 List <account> accounts = [SELECT Name FROM Account WHERE Id IN :accIds];
73
74 // make the new urls
75 Integer counter = 0;
76
77 // it is important to go through all the urls again, so that the order
78 // of the urls in the list is maintained.
79 for(PageReference mySalesforceUrl : mySalesforceUrls) {
80
81 //Get the URL of the page
82 String url = mySalesforceUrl.getUrl();
83
84 if(url.startsWith(ACCOUNT_VISUALFORCE_PAGE)){
85 myFriendlyUrls.add(new PageReference(ACCOUNT_PAGE + accounts.get(counter).name));
86 counter++;
87 } else {
88 //If this doesn't start like an account page,
89 //don't do any transformations
90 myFriendlyUrls.add(mySalesforceUrl);
91 }
92 }
93
94 //Return the full list of pages
95 return myFriendlyUrls;
96 }
97
98}Before and After Rewriting
- The original URL for the contact page before rewriting
- The link to the parent account page from the contact page
- The original URL for the link to the account page before rewriting, shown in the browser's status bar
- The rewritten URL for the contact page after rewriting
- The link to the parent account page from the contact page
- The rewritten URL for the link to the account page after rewriting, shown in the browser's status bar