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
DipakDipak 

How to use URL Rewriting without taking a constant prefix Text

I have tried a lot to fix out the “/cms/ ” issue,

 

For “/cms/” inside the code, we have sent PageReference as “/cms/Page-Category/PageName/”.

& then by the help of Salesforce's Site URL Rewritting feature this above page-Reference converted to internal URL like “/PreviewPageContainer?Id=PageID”

 

For this feature our previous-code look like this, Now just look in to the code,( specially with  highlighted lines)

 

 

global class SiteURLRewriter implements Site.UrlRewriter {

 

String DIRECTORY = '/cms/';

 

String VISUALFORCE_PAGE = '/PreviewPageContainer?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(DIRECTORY )){

 

String name;

Integer indx=url.lastIndexOf('/');

 

name=url.substring(0,url.length());

 

if(indx!=-1)

name=url.substring(indx+1,url.length());

 

Integer loc=name.indexOf('?');

String paramStr='';

 

if(loc!=-1){

name=name.substring(0,loc);

paramStr=url.substring(loc+1,url.length());

Page__c site_page = [select id,name from Page__c where name =:name LIMIT1];

return new PageReference(VISUALFORCE_PAGE +site_page.id+'&'+paramStr);

}

 

else{

 

Page__c site_page = [select id,name from Page__c where name =:name LIMIT 1];

return new PageReference(VISUALFORCE_PAGE + site_page.id);

}

 

}

 

return null;

 

}

 

// The second global method for mapping internal Ids to URLs

 

global List<PageReference> generateUrlFor(List<PageReference> mySalesforceUrls){

 

return null;

 

}

 

}

 

 

Now when we tried to remove the “/cms/” prefix, then we sent PageReference as “PageCategory/PageName/” & then by the help of Salesforce's Site URL Rewritting feature this above page-Reference converted to internal URL like “/PreviewPageContainer?Id=PageID”

 

Then also we have made some changes in SiteURLRewriter class like this,

 

Option-1 (Not Working)

 

1- remove String DIRECTORY = '/cms/';

2- Changed the condition if(url.startswith(DIRECTORY )) to if(url.length())>0

 

After this it is not working,

 

Option-2 (Not Working)

 

1- remain the String DIRECTORY = '/cms/'; code as it is

 

2- changed the condition to if(url.startswith('/'))

 

After this it is not working

 

Option-3 (Not Working)

 

1- remain the String DIRECTORY = '/cms/'; code as it is

 

2-changed the condition toif(url.length())>0

 

After this it is not working

 

 

 

 

Option-4 (Not Working)

 

1- remain the String DIRECTORY = '/sites/';

 

2-changed the condition toif(url.length())>0

 

After this it is not working

 

Option-5 ( Working)

 

1- remain the String DIRECTORY = '/sites/';

 

2-changed the condition toif(url.startswith(DIRECTORY ))

 

After this it is working

 

 

From these above test case,

 

I have noticed that if we change the condition “if(url.startswith(DIRECTORY ))” then it is not working and if we have placed any text in place of '/cms/' like /site/, and with same condition,then only it is working

 

 

Thanks

 

 

Dipak