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
Girish ParwaniGirish Parwani sites URL rewriter issue with URL parameter getting changed

i recently started on a project which uses sites and site has implemented URL Rewriter class, i was debugging an issue where URL parameter passed from one site page to another was getting changed, i wanted to know if there can be an issue with Rewriter class implementation :
global class SiteRewriterEP implements Site.UrlRewriter {
    //Variables to represent the user-friendly URLs for
    //JOB pages
    String JOB_PAGE = '/careers/';

    //Variables to represent my custom Visualforce pages
    String JOB_VISUALFORCE_PAGE = '/job_details?jid=';

    global PageReference mapRequestUrl(PageReference
        // Generice Method called for       
        PageReference returnPage = SiteRewriterHelper.mapURLRequested(myFriendlyUrl, JOB_PAGE, JOB_VISUALFORCE_PAGE);
        return returnPage;
    global List<PageReference> generateUrlFor(List<PageReference> 
        List<PageReference> returnPages = SiteRewriterHelper.generateURLRequested(mySalesforceUrls, JOB_PAGE, JOB_VISUALFORCE_PAGE);
        return returnPages;

global class SiteRewriterHelper {

    global static PageReference mapURLRequested(PageReference currentPagRef, 

        string url = currentPagRef.getUrl();
        system.debug(' @@ URL Rewriting 1 @@ ' + url);

        ts2__Job__c job = new ts2__Job__c();

            system.debug('+++ url is '+ url);
            String name = url.substring(FRIENDLY_PAGE.length(),
            String tempName = name;
            String parameters = '';     
                //integer i = name.indexOf('&');
                tempName = name.subString(0,name.indexOf('&'));
                parameters = name.substring(name.indexOf('&'), name.length());
            if(url.startsWith(FRIENDLY_PAGE) && (url.substring(FRIENDLY_PAGE.length(),url.length())) != null /*&& !url.contains('&')*/){
                job = [SELECT Id, Job_URL_Parameter__c  FROM ts2__Job__c WHERE Job_URL_Parameter__c =: tempName LIMIT 1];
            //Return a page in Visualforce format
            return new PageReference(VISUALFORCE_PAGE + + parameters);
            return null;

    global static list<PageReference> generateURLRequested(
    list<PageReference> currentPagReferences, String FRIENDLY_PAGE, 
        //A list of pages to return after all the links 
        //have been evaluated
        system.debug(' @@ URL Rewriting 2 @@ ' + currentPagReferences);
        List<PageReference> myFriendlyUrls = new List<PageReference>();
        //a list of all the ids in the urls
        List<id> jobIds = new List<id>();
        String parameters='';
        // loop through all the urls once, finding all the valid ids
        for(PageReference mySalesforceUrl : currentPagReferences){
            //Get the URL of the page            
            String url = mySalesforceUrl.getUrl();
            //If this looks like an page, transform it
                    //Extract the ID from the query parameter
                    //and store in a list
                    //for querying later in bulk.

                    String jid='';
                    String tempJID = url.substring(VISUALFORCE_PAGE.length(),
                        jid = tempJID.subString(0,tempJID.indexOf('&'));
                        parameters = tempJID.subString(tempJID.indexOf('&'), tempJID.length());
                        jid = tempJID;

        List <ts2__Job__c> jobs = new List <ts2__Job__c>();
        if(currentPagReferences.size() > 0 && currentPagReferences.get(0).getUrl().startsWith(VISUALFORCE_PAGE) /*&& !currentPagReferences.get(0).getUrl().contains('&') */){
            jobs = [SELECT Name, Job_URL_Parameter__c FROM ts2__Job__c WHERE Id IN :jobIds];
        // make the new urls
        Integer counter = 0;
        // it is important to go through all the urls again, so that the order
        // of the urls in the list is maintained. 
        for(PageReference mySalesforceUrl : currentPagReferences) {
            //Get the URL of the page
            String url = mySalesforceUrl.getUrl();

            myFriendlyUrls.add(new PageReference(FRIENDLY_PAGE + jobs.get(counter).Job_URL_Parameter__c + parameters));
            } else {            
        system.debug(' @@ URL Rewriting 3 @@ ' + myFriendlyUrls);
        //Return the full list of pages
        return myFriendlyUrls;